count of the multiple values in a python dictionary
Question:
I have a dictionary like the one below and I want to find out how many of the values in it are from the value entered by the user.
{'a': ['Sun', 'Jack'], 'b': ['John', 'Sun', 'Sarah'],...
Count how many 'Sun'
in dictionary
Expected output: 2
Answers:
You can use sum
.
dct = {'a': ['Sun'], 'b': ['John', 'Sun', 'Sarah']}
res = sum('Sun' in lst for lst in dct.values())
print(res)
# 2
I think the most straightforward way is to go through each value of the dict
dictionary = {'a': ['Sun', 'Jack'], 'b': ['John', 'Sun', 'Sarah']}
count = 0
for key in dictionary:
if 'Sun' in dictionary[key]:
count += 1
print(count)
Chain the all values of the dictionary with itertools.chain.from_iterable
, and then count with collections.Counter
:
>>> from collections import Counter
>>> from itertools import chain
>>> mapping = {'a': ['Sun', 'Jack'], 'b': ['John', 'Sun', 'Sarah']}
>>> Counter(chain.from_iterable(mapping.values()))['Sun']
2
Update:
For the new question you posted in the comment section:
>>> mp1 = {'a':['Sun'],'b':['John','Sun','Sarah']}
>>> mp2 = {'a':100,'b':80,'c':90 }
>>> keys = (k for k, v in mp1.items() if 'Sun' in v)
>>> max_key = max(keys, key=mp2.__getitem__)
>>> max_key, mp2[max_key]
('a', 100)
one can also use enumerate for counting.
d = {'a': ['Sun', 'Jack'], 'b': ['John', 'Sun', 'Sarah']}
max([i for i, l in enumerate(d.values(), 1) if "Sun" in l])
2
d1 = {‘a’: [‘Sun’], ‘b’: [‘John’, ‘Sun’, ‘Sarah’]}
d2 = {‘a’: 100, ‘b’: 80, ‘c’: 90 }
def max_val(key_w, d1, d2):
# prepare d2 for merging, by putting value numbers into lists
d2 = { k: [v] for k,v in d2.items()}
# merge the 2 dictionaries
d1_d2 = { key:d1.get(key,[])+d2.get(key,[]) for key in set(list(d1.keys())+list(d2.keys())) }
return sorted([(k, x.pop()) for k, x in list(d1_d2.items()) if key_w in x], key=lambda x: x[1]).pop()
max_val('Sun', d1, d2)
('a', 100)
I have a dictionary like the one below and I want to find out how many of the values in it are from the value entered by the user.
{'a': ['Sun', 'Jack'], 'b': ['John', 'Sun', 'Sarah'],...
Count how many 'Sun'
in dictionary
Expected output: 2
You can use sum
.
dct = {'a': ['Sun'], 'b': ['John', 'Sun', 'Sarah']}
res = sum('Sun' in lst for lst in dct.values())
print(res)
# 2
I think the most straightforward way is to go through each value of the dict
dictionary = {'a': ['Sun', 'Jack'], 'b': ['John', 'Sun', 'Sarah']}
count = 0
for key in dictionary:
if 'Sun' in dictionary[key]:
count += 1
print(count)
Chain the all values of the dictionary with itertools.chain.from_iterable
, and then count with collections.Counter
:
>>> from collections import Counter
>>> from itertools import chain
>>> mapping = {'a': ['Sun', 'Jack'], 'b': ['John', 'Sun', 'Sarah']}
>>> Counter(chain.from_iterable(mapping.values()))['Sun']
2
Update:
For the new question you posted in the comment section:
>>> mp1 = {'a':['Sun'],'b':['John','Sun','Sarah']}
>>> mp2 = {'a':100,'b':80,'c':90 }
>>> keys = (k for k, v in mp1.items() if 'Sun' in v)
>>> max_key = max(keys, key=mp2.__getitem__)
>>> max_key, mp2[max_key]
('a', 100)
one can also use enumerate for counting.
d = {'a': ['Sun', 'Jack'], 'b': ['John', 'Sun', 'Sarah']}
max([i for i, l in enumerate(d.values(), 1) if "Sun" in l])
2
d1 = {‘a’: [‘Sun’], ‘b’: [‘John’, ‘Sun’, ‘Sarah’]}
d2 = {‘a’: 100, ‘b’: 80, ‘c’: 90 }
def max_val(key_w, d1, d2):
# prepare d2 for merging, by putting value numbers into lists
d2 = { k: [v] for k,v in d2.items()}
# merge the 2 dictionaries
d1_d2 = { key:d1.get(key,[])+d2.get(key,[]) for key in set(list(d1.keys())+list(d2.keys())) }
return sorted([(k, x.pop()) for k, x in list(d1_d2.items()) if key_w in x], key=lambda x: x[1]).pop()
max_val('Sun', d1, d2)
('a', 100)