Python - Librería Copy

#30 ejemplos de cómo utilizar el módulo Copy en Python:[br]#1. Copiar una lista:[br]import copy[br]original_list = [1, 2, 3][br]new_list = copy.copy(original_list)[br]print(new_list)[br]#2. Copiar una lista anidada:[br]import copy[br]original_list = [[1, 2], [3, 4]][br]new_list = copy.copy(original_list)[br]print(new_list)[br]#3. Copiar una lista anidada de forma profunda:[br]import copy[br]original_list = [[1, 2], [3, 4]][br]new_list = copy.deepcopy(original_list)[br]print(new_list)[br]#4. Copiar un diccionario:[br]import copy[br]original_dict = {'a': 1, 'b': 2}[br]new_dict = copy.copy(original_dict)[br]print(new_dict)[br]#5. Copiar un diccionario de forma profunda:[br]import copy[br]original_dict = {'a': {'b': 1}}[br]new_dict = copy.deepcopy(original_dict)[br]print(new_dict)[br]#6. Copiar un objeto:[br]import copy[br]class MyClass:[br] def __init__(self, value):[br] self.value = value[br][br]original_obj = MyClass(1)[br]new_obj = copy.copy(original_obj)[br]print(new_obj)[br]#7. Copiar un objeto de forma profunda:[br]import copy[br]class MyClass:[br] def __init__(self, value):[br] self.value = value[br]original_obj = MyClass(1)[br]new_obj = copy.deepcopy(original_obj)[br]print(new_obj)[br]#8. Copiar una tupla:[br]import copy[br]original_tuple = (1, 2, 3)[br]new_tuple = copy.copy(original_tuple)[br]print(new_tuple)[br]#9. Copiar una tupla anidada:[br]import copy[br]original_tuple = ((1, 2), (3, 4))[br]new_tuple = copy.copy(original_tuple)[br]print(new_tuple)[br]#10. Copiar una tupla anidada de forma profunda:[br]import copy[br]original_tuple = ((1, 2), (3, 4))[br]new_tuple = copy.deepcopy(original_tuple)[br]print(new_tuple)[br]#11. Copiar un conjunto:[br]import copy[br]original_set = {1, 2, 3}[br]new_set = copy.copy(original_set)[br]print(new_set)[br]#12. Copiar un conjunto anidado:[br]import copy[br]original_set = {(1, 2), (3, 4)}[br]new_set = copy.copy(original_set)[br]print(new_set)[br]#13. Copiar un conjunto anidado de forma profunda:[br]import copy[br]original_set = {(1, 2), (3, 4)}[br]new_set = copy.deepcopy(original_set)[br]print(new_set)[br]#14. Copiar una cadena:[br]import copy[br]original_str = 'hello'[br]new_str = copy.copy(original_str)[br]print(new_str)[br]#15. Copiar una cadena anidada:[br]import copy[br]original_str = 'hello'[br]new_str = copy.copy(original_str)[br]print(new_str)[br]#16. Copiar una cadena anidada de forma profunda:[br]import copy[br]original_str = 'hello'[br]new_str = copy.deepcopy(original_str)[br]print(new_str)[br]#17. Copiar un archivo:[br]import copy[br]with open('original.txt', 'r') as f:[br] original_file = f.read()[br][br]with open('new.txt', 'w') as f:[br] new_file = copy.copy(original_file)[br] f.write(new_file)[br]#18. Copiar un archivo de forma profunda:[br]import copy[br]with open('original.txt', 'r') as f:[br] original_file = f.read()[br][br]with open('new.txt', 'w') as f:[br] new_file = copy.deepcopy(original_file)[br] f.write(new_file)[br]#19. Copiar un objeto personalizado:[br]import copy[br]class MyClass:[br] def __init__(self, value):[br] self.value = value[br][br] def __copy__(self):[br] return MyClass(self.value)[br][br]original_obj = MyClass(1)[br]new_obj = copy.copy(original_obj)[br]print(new_obj)[br]#20. Copiar un objeto personalizado de forma profunda:[br]import copy[br]class MyClass:[br] def __init__(self, value):[br] self.value = value[br][br] def __deepcopy__(self, memo):[br] return MyClass(copy.deepcopy(self.value, memo))[br][br]original_obj = MyClass(1)[br]new_obj = copy.deepcopy(original_obj)[br]print(new_obj)[br]#21. Copiar una lista de objetos personalizados:[br]import copy[br]class MyClass:[br] def __init__(self, value):[br] self.value = value[br][br]original_list = [MyClass(1), MyClass(2)][br]new_list = copy.copy(original_list)[br]print(new_list)[br]#22. Copiar una lista de objetos personalizados de forma profunda:[br]import copy[br]class MyClass:[br] def _init_(self, value):[br] self.value = value[br][br]original_list = [MyClass(1), MyClass(2)][br]new_list = copy.deepcopy(original_list)[br]print(new_list)[br]#23. Copiar un diccionario de objetos personalizados:[br]import copy[br]class MyClass:[br] def _init_(self, value):[br] self.value = value[br][br]original_dict = {'a': MyClass(1), 'b': MyClass(2)}[br]new_dict = copy.copy(original_dict)[br]print(new_dict)[br]#24. Copiar un diccionario de objetos personalizados de forma profunda:[br]import copy[br]class MyClass:[br] def _init_(self, value):[br] self.value = value[br][br]original_dict = {'a': MyClass(1), 'b': MyClass(2)}[br]new_dict = copy.deepcopy(original_dict)[br]print(new_dict)[br]#25. Copiar una lista de diccionarios:[br]import copy[br]original_list = [{'a': 1}, {'b': 2}][br]new_list = copy.copy(original_list)[br]print(new_list)[br]#26. Copiar una lista de diccionarios de forma profunda:[br]import copy[br]original_list = [{'a': 1}, {'b': 2}][br]new_list = copy.deepcopy(original_list)[br]print(new_list)[br]#27. Copiar un diccionario de listas:[br]import copy[br]original_dict = {'a': [1, 2], 'b': [3, 4]}[br]new_dict = copy.copy(original_dict)[br]print(new_dict)[br]#28. Copiar un diccionario de listas de forma profunda:[br]import copy[br]original_dict = {'a': [1, 2], 'b': [3, 4]}[br]new_dict = copy.deepcopy(original_dict)[br]print(new_dict)[br]#29. Copiar una lista de conjuntos:[br]import copy[br]original_list = [{1, 2}, {3, 4}][br]new_list = copy.copy(original_list)[br]print(new_list)[br]#30. Copiar una lista de conjuntos de forma profunda:[br]import copy[br]original_list = [{1, 2}, {3, 4}][br]new_list = copy.deepcopy(original_list)[br]print(new_list)
Geogebra Python

Information: Python - Librería Copy