Skip to content

Instantly share code, notes, and snippets.

@Everfighting
Created April 17, 2018 00:48
Show Gist options
  • Save Everfighting/4cc35b47a6cd308082983944a733dffa to your computer and use it in GitHub Desktop.
Save Everfighting/4cc35b47a6cd308082983944a733dffa to your computer and use it in GitHub Desktop.
defaultdict用法
# defaultdict Examples
# Using list as the default_factory, it is easy to group a sequence of key-value pairs into a dictionary of lists:
s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
d = defaultdict(list)
for k, v in s:
d[k].append(v)
sorted(d.items())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment