함수 collection_dot()

  다른 뜻에 대해서는 함수 array_dot() 문서를 참조하십시오.

1 개요[ | ]

함수 dot()
함수 object_dot()
함수 collection_dot()

2 Python[ | ]

Python 3
def dot( o, prepend='' ):
    results = {}
    if isinstance(o, dict):
        for key in o:
            value = o[key]
            results.update( dot(value, prepend + '.' + key ) )
    elif isinstance(o, list):
        for idx, value in enumerate(o):
            results.update( dot(value, prepend + '['+str(idx)+']') )
    else:
        results[prepend] = o
    return results

print( dot({'foo':{'bar':'baz'}}) )
# {'.foo.bar': 'baz'}
print( dot({'name':{'first':'Yonezawa','last':'Akinori'},'tel':'1234-5678'}) )
# {'.name.first': 'Yonezawa', '.name.last': 'Akinori', '.tel': '1234-5678'}
print( dot({'name':{'first':'Yonezawa','last':'Akinori'},'tel':'1234-5678','fruits':['apple','banana']}) )
# {'.name.first': 'Yonezawa', '.name.last': 'Akinori', '.tel': '1234-5678', '.fruits[0]': 'apple', '.fruits[1]': 'banana'}
Python 2
import types
def dot( o, prepend='' ):
    results = {}
    if isinstance(o, types.DictType):
        for key in o:
            value = o[key]
            results.update( dot(value, prepend + '.' + key ) )
    elif isinstance(o, types.ListType):
        for idx, value in enumerate(o):
            results.update( dot(value, prepend + '['+str(idx)+']') )
    else:
        results[prepend] = o
    return results
    
print( dot({'foo':{'bar':'baz'}}) )
# {'.foo.bar': 'baz'}
print( dot({'name':{'first':'Yonezawa','last':'Akinori'},'tel':'1234-5678'}) )
# {'.name.first': 'Yonezawa', '.name.last': 'Akinori', '.tel': '1234-5678'}
print( dot({'name':{'first':'Yonezawa','last':'Akinori'},'tel':'1234-5678','fruits':['apple','banana']}) )
# {'.name.first': 'Yonezawa', '.name.last': 'Akinori', '.fruits[1]': 'banana', '.tel': '1234-5678', '.fruits[0]': 'apple'}

3 같이 보기[ | ]

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