Skip to content

Instantly share code, notes, and snippets.

@yuitest
Created May 5, 2014 13:11
Show Gist options
  • Save yuitest/ea542eca3403a3ce0821 to your computer and use it in GitHub Desktop.
Save yuitest/ea542eca3403a3ce0821 to your computer and use it in GitHub Desktop.
Dict と List のネストしたものから、プリミティブなものを取り出す。(例えば JSON とか。)
# coding: utf8
import types
def unstructure(structured, usedictkey=True, usedictvalue=True):
if isinstance(structured, dict):
for keys, values in structured.iteritems():
if usedictkey:
for key in unstructure(keys):
yield key
if usedictvalue:
for value in unstructure(values):
yield value
elif isinstance(structured, (list, tuple, xrange, types.GeneratorType)):
for items in structured:
for item in unstructure(items):
yield item
else:
yield structured
if __name__ == '__main__':
test = {
'x': ['y', 'z'],
'z': [
{
'w': {
's': ['a']
}
}
]
}
from collections import Counter
c = Counter()
for token in unstructure(test):
c[token] += 1
assert dict(c) == {'x': 1, 'y': 1, 'z': 2, 'w': 1, 's': 1, 'a': 1}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment