python中字典的复制

 

1、

>>> test1 = dict(key1="xxx",key2="aaa",key3=100,key="abc")
>>> test1
{'key1': 'xxx', 'key2': 'aaa', 'key3': 100, 'key': 'abc'}
>>> type(test1)
<class 'dict'>
>>> len(test1)
4
>>> test2={}
>>> for i,j in test1.items():
    test2[i] = j

    
>>> test2
{'key1': 'xxx', 'key2': 'aaa', 'key3': 100, 'key': 'abc'}
>>> test1
{'key1': 'xxx', 'key2': 'aaa', 'key3': 100, 'key': 'abc'}

 

2、

>>> test1 = {"key1":"aaa","key2":"bbb","key3":"ccc"}
>>> test1
{'key1': 'aaa', 'key2': 'bbb', 'key3': 'ccc'}
>>> test2 = test1.copy()
>>> test2
{'key1': 'aaa', 'key2': 'bbb', 'key3': 'ccc'}

 

posted @ 2021-01-01 17:50  小鲨鱼2018  阅读(241)  评论(0编辑  收藏  举报