Answer the question
In order to leave comments, you need to log in
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()
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
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 )
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question