python dictionary is a list or dictionary construction manner

1 , the value of the list constructor

dic = {}

dic.setdefault(key,[]).append(value)

>>dic.setdefault('a',[]).append(1)
>>dic.setdefault('a',[]).append(2)
>>dic
>>{'a': [1, 2]}

 

2 , the value of the dictionary constructor

dic = {}

dic.setdefault(key,{})[value] =1

>>dic.setdefault('b',{})['f']=1
>>dic.setdefault('b',{})['h']=1
>>dic.setdefault('b',{})['g']=1
>>dic
>>{'b': {'h': 1, 'g': 1, 'f': 1}}

Guess you like

Origin www.cnblogs.com/go-ahead-wsg/p/12130991.html