在Python中设置 copy()
copy() 方法返回Python中集合的浅表副本。如果我们使用“=”将一个集合复制到另一个集合,当我们在复制的集合中进行修改时,更改也会反映在原始集合中。所以我们必须创建该集合的浅表副本,这样当我们修改复制集合中的某些内容时,更改不会反映在原始集合中。
句法:
set_name.copy()
set_name: Name of the set whose copy
we want to generate.
参数:集合的 copy() 方法不带任何参数。
返回值:该函数返回原始集合的浅拷贝。
下面是上述函数的实现:
# Python3 program to demonstrate the use
# of join() function
set1 = {1, 2, 3, 4}
# function to copy the set
set2 = set1.copy()
# prints the copied set
print(set2)
输出:
{1, 2, 3, 4}
浅拷贝示例:
# Python program to demonstrate that copy
# created using set copy is shallow
first = {'g', 'e', 'e', 'k', 's'}
second = first.copy()
# before adding
print 'before adding: '
print 'first: ',first
print 'second: ', second
# Adding element to second, first does not
# change.
second.add('f')
# after adding
print 'after adding: '
print 'first: ', first
print 'second: ', second
输出:
before adding:
first: set(['s', 'e', 'k', 'g'])
second: set(['s', 'e', 'k', 'g'])
after adding:
first: set(['s', 'e', 'k', 'g'])
second: set(['s', 'e', 'k', 'g', 'f'])