I need the following:
deet = [{
'title': 'T-90',
'timestamp': '2022-10-08T21:01:51Z'},
{
'title': 'Category:17th-century women of the Ottoman Empire',
'timestamp': '2022-10-08T21:01:50Z'},
{
'title': 'T-90',
'timestamp': '2022-10-08T21:01:51Z'},
{
'title': 'T-90',
'timestamp': '2022-10-08T21:01:51Z'},
{
'title': 'T-91',
'timestamp': '2022-10-08T21:01:51Z'}
]
from collections import Counter
print([new["title"] for new in deet].Counter())
this is a part of the data, deet but it is the same structure. i tried to use counter method from collections but i am really getting errors. i need the frequency of the values of title, that is how many times T-90 is in the list.
One way to do this is to use a defaultdict from the collections library to count the items. The defaultdict can then be sorted by item count using a lambda function which returns the value for each of the defaultdict's keys. Note that the flag reverse=True
returns highest to lowest in this case.
from collections import defaultdict
dd = defaultdict(int)
for item in deet:
dd[item.get('title')] += 1
sorted(dd, key=lambda k: dd[k], reverse=True)
Using Counter from the same collections library is also a good solution; in this case you need to get each item's title from each dict in the list.
from collections import Counter
Counter([item['title'] for item in deet])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.