In this article, let’s learn about the python copy function in the python dictionary. What does python copy mean? What functions does it have? Get the answer in the article below.
Summary description
Python Dictionary copy() function returns a shallow copy of the dictionary.
Syntax
copy() method syntax:
dict.copy()
Parameters
Return value
Example
The following example shows how to use the copy() function:# !/usr/bin/python dict1 = {'Name': 'Zara', 'Age': 7}; dict2 = dict1.copy() print "New Dictinary : %s" % str(dict2)
New Dictinary : {'Age': 7, 'Name': 'Zara'}
The difference between direct assignment and copy
# !/usr/bin/python # -*- coding: UTF-8 -*- dict1 = {'user': 'runoob', 'num': [1, 2, 3]} dict2 = dict1 # 浅拷贝: 引用对象 dict3 = dict1.copy() # 浅拷贝:深拷贝父对象(一级目录),子对象(二级目录)不拷贝,还是引用 # 修改 data 数据 dict1['user'] = 'root' dict1['num'].remove(1) # 输出结果 print(dict1) print(dict2) print(dict3)
{'num': [2, 3], 'user': 'root'} {'num': [2, 3], 'user': 'root'} {'num': [2, 3], 'user': 'runoob'}
Python tutorial column on the php Chinese website.
The above is the detailed content of What is the copy function? What is the difference between direct assignment and copy?. For more information, please follow other related articles on the PHP Chinese website!