How to get top 3 count of names from list of dictionary in python

psw

Hello guys,

 resp = [{**"NR": "0"**,"Code": "4_RESOURCE","Cnt": "11"}, 
                {"NR": "10","Code": "10_humans","Cnt": "1"},
                {"NR": "1000","Code": "4_RESOURCE","Cnt": "120"}, 
                {**"NR": "0"**,"Code": "10_humans","Cnt": "12"},
                 {**"NR": "0"**,"Code": "4_RESOURCE","Cnt": "15"},
                 {**"NR": "0"**,"Code": "50_animals","Cnt": "20"}]

from it, if "NR" is "0" from unique Code from the above list of dictionary then need to take count and add that count against the unique code. like that need to take top three counts. it should be in dictionary as shown in output sample

output sample: count of Cnt {"4_RESOURCE": 26(15+11), "4_RESOURCE": 15, "10_humans":12} -------[Top three in dictionary]

I tired:

 def se_conc(response):
    cnt = 0
    list = []

    def key_func(k):
         return k['Code']
   INFO = sorted(resp, key=y_func)
   for k, v in groupby(INFO, y_func):
            print("codes:", k)
            print("v:", v)
            list.append(k)
            print("listcode", list)

            for key in list:
                if resp['NR'] == "0":
                    print("NR is 0:", k)

Thanks in advance

schwobaseggl

Use a collections.Counter and its most_commons method:

from collections import Counter

c = Counter()
for d in resp:
    if d["NR"] == "0":
        c[d["Code"]] += int(d["Cnt"])

dict(c.most_common(3))
# {'4_RESOURCE': 26, '50_animals': 20, '10_humans': 12}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get the key and value pair from a list of dictionary in python 3

From Dev

How to get a value from a list of dictionary Python?

From Dev

How to get top 10 and ORDER BY() from COUNT()

From Dev

Python 3: How to add elements from list as values of a dictionary?

From Dev

Sorting a dictionary and get top 3

From Dev

get function names from a list python

From Dev

Python : Get value from a list of Json dictionary

From Java

How to get dictionary value from key in a list?

From Dev

How to get data from list with dictionary

From Dev

Difficulty getting the item count for the combinations of list of items from python dictionary

From Dev

How to get index of a sorted list of dictionary in python?

From Dev

Python 3 - How to convert a string to a list of dictionary

From Dev

how to get array from dictionary in swift 3

From Dev

How to get two sums of number of fields from a list of names: one with 2 fields, the other with 3 or more?

From Dev

How to create a nested dictionary from a list in Python?

From Dev

Get a list of a dictionary's key names

From Dev

How to count values in a list Python 3

From Dev

How to get a dictionary from a json file in Python?

From Dev

how to get the items from the dictionary python

From Dev

How to get value from nested dictionary in python?

From Dev

How do I get a list from set in python 3

From Dev

How to get all value from multiple dictionary with same value into list python

From Dev

How to get a value in a dictionary inside a list in python and further index a value from it

From Dev

How to read the first line from a file as key and the next 3 lines as a list of values to a dictionary, python

From Dev

get count of repeated values in a list of dictionary

From Dev

Get a list of file names from HDFS using python

From Dev

python pandas: find the top 3 frequent names

From Java

Getting the top 3 numbers in a list from infile - python

From Dev

list file names from a folder to a tkinter window, with python 3

Related Related

  1. 1

    How to get the key and value pair from a list of dictionary in python 3

  2. 2

    How to get a value from a list of dictionary Python?

  3. 3

    How to get top 10 and ORDER BY() from COUNT()

  4. 4

    Python 3: How to add elements from list as values of a dictionary?

  5. 5

    Sorting a dictionary and get top 3

  6. 6

    get function names from a list python

  7. 7

    Python : Get value from a list of Json dictionary

  8. 8

    How to get dictionary value from key in a list?

  9. 9

    How to get data from list with dictionary

  10. 10

    Difficulty getting the item count for the combinations of list of items from python dictionary

  11. 11

    How to get index of a sorted list of dictionary in python?

  12. 12

    Python 3 - How to convert a string to a list of dictionary

  13. 13

    how to get array from dictionary in swift 3

  14. 14

    How to get two sums of number of fields from a list of names: one with 2 fields, the other with 3 or more?

  15. 15

    How to create a nested dictionary from a list in Python?

  16. 16

    Get a list of a dictionary's key names

  17. 17

    How to count values in a list Python 3

  18. 18

    How to get a dictionary from a json file in Python?

  19. 19

    how to get the items from the dictionary python

  20. 20

    How to get value from nested dictionary in python?

  21. 21

    How do I get a list from set in python 3

  22. 22

    How to get all value from multiple dictionary with same value into list python

  23. 23

    How to get a value in a dictionary inside a list in python and further index a value from it

  24. 24

    How to read the first line from a file as key and the next 3 lines as a list of values to a dictionary, python

  25. 25

    get count of repeated values in a list of dictionary

  26. 26

    Get a list of file names from HDFS using python

  27. 27

    python pandas: find the top 3 frequent names

  28. 28

    Getting the top 3 numbers in a list from infile - python

  29. 29

    list file names from a folder to a tkinter window, with python 3

HotTag

Archive