Skip to content

Instantly share code, notes, and snippets.

@fereria
Created January 25, 2018 13:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fereria/c7b231bd5a67a3b80871cf1e186f8d26 to your computer and use it in GitHub Desktop.
Save fereria/c7b231bd5a67a3b80871cf1e186f8d26 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
from collections import defaultdict
src = [('a',1),('b',2),('c',3),('a',3)]
# 使わないとき
d = defaultdict(list)
result = {}
for i,j in src:
if i in result:
result[i].append(j)
else:
result[i] = [j]
print result
# defaultdict
for i,j in src:
d[i].append(j)
print d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment