파이썬 dict_dot()

1 개요[ | ]

Python dict_dot()
파이썬 dict_dot()
Python 3
def dict_dot( d, prepend='' ):
  results = {}
  for key in d:
    value = d[key]
    if isinstance(value, dict): results.update( dict_dot(value, prepend + key + '.') )
    else: results[prepend + key] = value
  return results

print( dict_dot({'foo':{'bar':'baz'}}) )
# {'foo.bar': 'baz'}
print( dict_dot({'name':{'first':'Yonezawa','last':'Akinori'},'tel':'1234-5678'}) )
# {'name.last': 'Akinori', 'name.first': 'Yonezawa', 'tel': '1234-5678'}
Python 2
import types
def dict_dot( d, prepend='' ):
  results = {}
  for key in d:
    value = d[key]
    if isinstance(value, types.DictType): results.update( dict_dot(value, prepend + key + '.') )
    else: results[prepend + key] = value
  return results

print( dict_dot({'foo':{'bar':'baz'}}) )
# {'foo.bar': 'baz'}
print( dict_dot({'name':{'first':'Yonezawa','last':'Akinori'},'tel':'1234-5678'}) )
# {'name.last': 'Akinori', 'name.first': 'Yonezawa', 'tel': '1234-5678'}

2 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}