“Salinan Python” Kode Jawaban

Objek klon Python

import copy

new_ob = copy.deepcopy(old_ob)
Sasso

Python Deepcopy

>>> import copy
>>> nums = [1, 2, 3]
>>> data = {'a': 10, 'b': nums}
>>> data
{'a': 10, 'b': [1, 2, 3]}
>>> data_copy = copy.copy(data)
>>> data_deep = copy.deepcopy(data)
>>> data_copy
{'a': 10, 'b': [1, 2, 3]}
>>> data_deep
{'a': 10, 'b': [1, 2, 3]}
>>> data_copy['a'] += 2
>>> nums[1:1] = [254]
>>> data
{'a': 10, 'b': [1, 254, 2, 3]}
>>> data_copy
{'a': 12, 'b': [1, 254, 2, 3]}
>>> data_deep
{'a': 10, 'b': [1, 2, 3]}
Attractive Albatross

CopyFile Pyhon

from shutil import copyfile
copyfile(src, dst)
Better Bug

Salinan Python

org_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
new_list = org_list.copy()

# For lists in lists you need deepcopy()

import copy

org_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
new_list = copy.deepcopy(org_list)
 
Said HR

Jawaban yang mirip dengan “Salinan Python”

Pertanyaan yang mirip dengan “Salinan Python”

Lebih banyak jawaban terkait untuk “Salinan Python” di Python

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya