G
G
guruvan_damm2021-06-07 22:36:56
Python
guruvan_damm, 2021-06-07 22:36:56

How to count list values ​​by key?

Hello! Using api, I unload a specific list of all_tickets, which has a key:value binding.
You need to count how many times the assignee_id key occurs and sort in descending order.
Example:
186485 - 220 times
184002 - 124 times

This is how I unload the list:

def take_all_chats():
    token = "blablabla"
    offset = 0
    all_tickets = []

    while offset < 20:
        responce = requests.get('blablabla.com',
                                params={
                                    'api_token': token,
                                    'updated_before': '2021-07-06 23:59',
                                    'updated_after': '2021-06-06 23:59',
                                    'fstatus': '3',
                                    'fchannel': '25222',
                                    'offset': offset
                                })
        data = responce.json()
        offset += 1
        all_tickets.extend(data)
        time.sleep(1)

    print(counts)


take_all_chats()


I tried to upload the quantity like this, but it did not work:
values_per_key = {}
    for d in all_tickets:
        for k, v in d.items():
            values_per_key.setdefault('assignee_id', set()).add(v)
    counts = {k: len(v) for k, v in values_per_key.items()}

    print(counts)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2021-06-07
@guruvan_damm

From phone, but should work)

values_per_key = {}
for d in all_tickets:
    for k, v in d.items():
        if k == "assigned_id":
            if not values_per_key.get(v):
                values_per_key[v] = 1
            else:
                values_per_key[v] += 1    
print(values_per_key )

A
aRegius, 2021-06-08
@aRegius

Counter(data[key] for data in all_tickets if data.get(key, None))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question