Skip to content

Instantly share code, notes, and snippets.

@imom0
Created November 19, 2012 04:21
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 imom0/4108933 to your computer and use it in GitHub Desktop.
Save imom0/4108933 to your computer and use it in GitHub Desktop.
Count tuple-key dict element appearence
# url http://stackoverflow.com/questions/13447226/count-how-many-times-a-part-of-a-key-appears-in-a-dictionary-python
from collections import Counter
from itertools import chain
a = { (1,2):3, (1,3):5, (2,1):6 }
# first
# nested iter, order is important
Counter(j for k in a for j in k)
# second
# use chain of itertools
Counter(chain(*a.keys()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment