Created
May 5, 2014 13:11
-
-
Save yuitest/ea542eca3403a3ce0821 to your computer and use it in GitHub Desktop.
Dict と List のネストしたものから、プリミティブなものを取り出す。(例えば JSON とか。)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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