字典由多个键及与其对应的值构成的键值对构成,字典中键唯一,值不唯一。
1)dict 函数:
>>>items=[('name','lilei'),('age',12)]
>>>d=dict(items)
>>>d
{'age':12,'name':'lilei'} >>>d=dict(name='lilei',age=12)
>>>d
{'age':12,'name':'lilei'}
2) copy & deep copy
>>>a={}
>>>a['names']=['lilei','lily']
>>>b=a.copy()
>>>b['names'].remove('lily')
>>>b
{''names':['b']}
>>>a
{''names':['b']}
可以看到,副本中修改值后原始字典表也会改变,避免这个问题的方法是使用深复制:
>>>a={}
>>>a['names']=['lilei','lily']
>>>from copy import deepcopy
>>>c=deepcopy(a)
>>>c['names'].remove('lily')
>>>c
{''names':['b']}
>>>a
{''names':['a','b']}