python 中的copy()
函數有助于創建集合的副本。我們可以說它返回了一個淺拷貝,這意味著新集合中的任何更改都不會反映原始集合。
**set.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'}