Reputation:
I have a file with type
= collections.defaultdict
it is like this:
defaultdict(set,
{'2712': {'java', 'manager', 'program', 'programme', 'senior'},
'3193': {'agile', 'coach', 'master', 'scrum'},
'2655': {'business', 'consultant', 'principal'})
I have never worked with this type. How I can count each elements for each keys? for example for '2712' is 5 for 3193
is 4 and for 2655
is 3.
Upvotes: 0
Views: 1439
Reputation: 14273
for key, value in some_dict.items():
print(f'{key}: {len(value)}')
where some_dict
is your dict/defaultdict.
Upvotes: 1
Reputation: 6732
As the docs for defaultdict
say, you can use a default dict in the same way as a normal dict, using the square brackets for indexing for keys, and iterating through the list of keys with a for loop, as below:
d = defaultdict(
set, {
'2712': {'java', 'manager', 'program', 'programme', 'senior'},
'3193': {'agile', 'coach', 'master', 'scrum'},
'2655': {'business', 'consultant', 'principal'}
})
for key in d:
print(f"The set at key {key} is {len(d[key])} long.")
See repl here
Upvotes: 1