Python copy()
更新:2022-07-24 13:04
python
中的copy()
函数有助于创建集合的副本。我们可以说它返回了一个浅拷贝,这意味着新集合中的任何更改都不会反映原始集合。
set.copy()
copy()参数:
copy()
方法不接受任何参数。
copy()返回值
有时我们使用=
运算符来复制集合,不同之处在于=
运算符创建对集合的引用,而copy()
创建新的集合。
投入 | 返回值 |
---|---|
设置 | 浅拷贝 |
Python 中copy()
方法的示例
示例copy()
方法如何对 Python 中的集合起作用?
alphabet = {'a', 'b', 'c'}
new_set = alphabet .copy()
new_set.add(d)
print('Alphabets : ', alphabet )
print('New Set: ', new_set)
输出:
Alphabets: {'a', 'b', 'c'}
New Set : {'a', 'b', 'c', 'd'}
示例 2:在 Python 中,copy()
方法如何使用=
运算符处理集合?
alphabet = {'a', 'b', 'c'}
new_set = alphabet
new_set.add(d)
print('Alphabets : ', alphabet )
print('New Set: ', new_set)
输出:
Alphabets: {'a', 'b', 'c'}
New Set : {'a', 'b', 'c', 'd'}