Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get a count of dictionary keys with values greater than some integer in python

I have a dictionary. The keys are words the value is the number of times those words occur.

countDict = {'house': 2, 'who': 41, 'joey': 409, 'boy': 2, 'girl':2}

I'd like to find out how many elements occur with a value of more than 1, with a value of more than 20 and with a value of more than 50.

I found this code

a = sum(1 for i in countDict if countDict.values() >= 2)

but I get an error that I'm guessing means that values in dictionaries can't be processed as integers.

builtin.TypeError: unorderable types: dict_values() >= int()

I tried modifying the above code to make the dictionary value be an integer but that did not work either.

a = sum(1 for i in countDict if int(countDict.values()) >= 2)

builtins.TypeError: int() argument must be a string or a number, not 'dict_values'

Any suggestions?

like image 416
Amanda Avatar asked May 03 '17 01:05

Amanda


People also ask

How to count the values associated with key in Python dictionary?

Write a Python program to count the values associated with key in a dictionary. student = [{'id': 1, 'success': True, 'name': 'Lary'}, {'id': 2, 'success': False, 'name': 'Rabi'}, {'id': 3, 'success': True, 'name': 'Alex'}] print(sum( d ['id'] for d in student)) print(sum( d ['success'] for d in student))

How to count multiple objects at once in Python?

To count several different objects at once, you can use a Python dictionary. The dictionary keys will store the objects you want to count. The dictionary values will hold the number of repetitions of a given object, or the object’s count.

How to sum a number greater than k in Python?

The sum () can also help us achieving this task. We can return 1 when the number greater than k is found and then compute the summation of is using sum ()

How to solve the Dictionary counter problem in Python?

Let’s discuss certain ways in which this problem can be solved. This problem can be solved using naive method of loop. In this we just iterate through each key in dictionary and when a match is found, the counter is increased.


3 Answers

countDict.items() gives you key-value pairs in countDict so you can write:

>>> countDict = {'house': 2, 'who': 41, 'joey': 409, 'boy': 2, 'girl':2}
>>> [word for word, occurrences in countDict.items() if occurrences >= 20]
['who', 'joey']

If you just want the number of words, use len:

>>> countDict = {'house': 2, 'who': 41, 'joey': 409, 'boy': 2, 'girl':2}
>>> wordlist = [word for word, occurrences in countDict.items() if occurrences >= 20]
>>> len(wordlist)
2

Note that Python variables use lowercase and underscores (snake case): count_dict rather than countDict. By convention camel case is used for classes in Python:

breakfast = SpamEggs()  # breakfast is new instance of class SpamEggs
lunch = spam_eggs()  # call function spam_eggs and store result in lunch
dinner = spam_eggs  # assign value of spam_eggs variable to dinner

See PEP8 for more details.

like image 193
Nick Avatar answered Oct 11 '22 12:10

Nick


You need this:

>>> countDict = {'house': 2, 'who': 41, 'joey': 409, 'boy': 2, 'girl':2}

>>> sum(1 for i in countDict.values() if i >= 2)
5

values() returns a list of all the values available in a given dictionary which means you can't convert the list to integer.

like image 29
McGrady Avatar answered Oct 11 '22 13:10

McGrady


You could use collections.Counter and a "classification function" to get the result in one-pass:

def classify(val):
    res = []
    if val > 1:
        res.append('> 1')
    if val > 20:
        res.append('> 20')
    if val > 50:
        res.append('> 50')
    return res

from collections import Counter

countDict = {'house': 2, 'who': 41, 'joey': 409, 'boy': 2, 'girl':2}
Counter(classification for val in countDict.values() for classification in classify(val))
# Counter({'> 1': 5, '> 20': 2, '> 50': 1})

Of course you can alter the return values or thresholds in case you want a different result.


But you were actually pretty close, you probably just mixed up the syntax - correct would be:

a = sum(1 for i in countDict.values() if i >= 2)

because you want to iterate over the values() and check the condition for each value.

What you got was an exception because the comparison between

>>> countDict.values()
dict_values([2, 409, 2, 41, 2])

and an integer like 2 doesn't make any sense.

like image 2
MSeifert Avatar answered Oct 11 '22 13:10

MSeifert