字典是无序,可更改和索引的集合。在Python中,字典用大括号括起来,并且具有键和值。他们copy()
方法返回字典的浅表副本。
#creating a dictionary original = {1:'vishesh', 2:'python'} # copying using copy() function new = original.copy() # removing all elements from the list Only new list becomes empty as #copy() does shallow copy. new.clear() print('new: ', new) print('original: ', original) # between = and copy()original = {1:'Vishesh', 2:'python'} # copying using copy() function new = original.copy() # removing all elements from new list # and printing both new.clear() print('new: ', new) print('original: ', original) original = {1:'one', 2:'two'} # copying using = new = original # removing all elements from new list # and printing both new.clear() print('new: ', new) print('original: ', original)
输出结果
('new: ', {}) ('original: ', {1: 'vishesh', 2: 'python'}) ('new: ', {}) ('original: ', {1: 'Vishesh', 2: 'python'}) ('new: ', {}) ('original: ', {})