Hello everyone I have an unique question.
In a key:value dictionary, how do I get the len
of an item list and let the len
be the keys value?
Such as
D = {'Chicago Cubs': 1907, 1908, 'World Series Not Played in 1904': [1904],
'Boston Americans': 1903, 'Arizona Diamondbacks': 2001,
'Baltimore Orioles':1966, 1970, 1983}
Chicago Cubs: 1907,1908
team = key and number of times shown = value
After counting the number of times the item appears in
Chicago Cubs - 2
What I need is:
Chicago Cubs: 2
What I have is:
D = {}
for e in l:
if e[0] not in D:
D[e[0]] = [e[1]]
else:
D[e[0]].append(e[1])
for k, v in sorted(D.items(), key=lambda x: -len(x[1])):
max_team = ("%s - %s" % (k,len(v)))
print(max_team)
return max_team
How should I go about this?