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