Using deepcopy() or copy() is a good solution. For a simple 2D-array case
y = [row[: ] for row in x ]
Try this:
from copy
import copy, deepcopy
y = deepcopy(x)
For 2D arrays it's possible use map function:
old_array = [ [2, 3], [4, 5] ] # python2.* new_array = map(list, old_array) # python3.* new_array = list(map(list, old_array))
Note that sample below is simply intended to show you an example(don't beat me to much) how deepcopy could be implemented for 1d and 2d arrays:
arr = [
[1, 2],
[3, 4]
]
deepcopy1d2d = lambda lVals: [x
if not isinstance(x, list)
else x[: ]
for x in lVals
]
dst = deepcopy1d2d(arr)
dst[1][1] = 150
print dst
print arr
I think np.tile also might be useful
>>> a = np.array([0, 1, 2]) >>> np.tile(a, 2) array([0, 1, 2, 0, 1, 2]) >>> np.tile(a, (2, 2)) array([ [0, 1, 2, 0, 1, 2], [0, 1, 2, 0, 1, 2] ]) >>> np.tile(a, (2, 1, 2)) array([ [ [0, 1, 2, 0, 1, 2] ], [ [0, 1, 2, 0, 1, 2] ] ])
Last Updated : 30 Dec, 2020
The original list is: [ [5, 6, 8], [8, 5, 3], [9, 10, 3] ] The Matrix after duplicates removal is: [ [5, 6, 8], [3], [9, 10] ]
In this tutorial, we will learn about the Python List copy() method with the help of examples.,The copy() method returns a shallow copy of the list.,The copy() method returns a new list. It doesn't modify the original list.,However, if you need the original list unchanged when the new list is modified, you can use the copy() method.
Example
# mixed list prime_numbers = [2, 3, 5] # copying a list numbers = prime_numbers.copy() print('Copied List:', numbers) # Output: Copied List: [2, 3, 5]
Example
# mixed list prime_numbers = [2, 3, 5] # copying a list numbers = prime_numbers.copy() print('Copied List:', numbers) # Output: Copied List: [2, 3, 5]
The syntax of the copy()
method is:
new_list = list.copy()
Example: Copying a List
# mixed list my_list = ['cat', 0, 6.7] # copying a list new_list = my_list.copy() print('Copied List:', new_list)
We can also use the =
operator to copy a list. For example,
old_list = [1, 2, 3] new_list = old_list
Howerver, there is one problem with copying lists in this way. If you modify new_list, old_list is also modified. It is because the new list is referencing or pointing to the same old_list object.
old_list = [1, 2, 3] # copy list using = new_list = old_list # add an element to list new_list.append('a') print('New List:', new_list) print('Old List:', old_list)
STEP 1: Declare and initialize an array.,Classroom and Online Training
Duplicate elements in given array: 2 3 8
If True, then sub-classes will be passed-through, otherwise the returned array will be forced to be a base-class array (defaults to False).,Return an array copy of the given object.,Note that np.copy is a shallow copy and will not copy object elements within arrays. This is mainly important for arrays containing Python objects. The new array will contain the same object which may lead to surprises if that object can be modified (is mutable):,To ensure all elements within an object array are copied, use copy.deepcopy:
>>> np.array(a, copy = True)
>>> x = np.array([1, 2, 3]) >>> y = x >>> z = np.copy(x)
>>> x[0] = 10 >>> x[0] == y[0] True >>> x[0] == z[0] False
>>> a = np.array([1, 2, 3]) >>>
a.flags["WRITEABLE"] = False >>>
b = np.copy(a) >>>
b.flags["WRITEABLE"]
True
>>>
b[0] = 3 >>>
b
array([3, 2, 3])
>>> a = np.array([1, 'm', [2, 3, 4]], dtype = object) >>>
b = np.copy(a) >>>
b[2][0] = 10 >>>
a
array([1, 'm', list([10, 3, 4])], dtype = object)
>>>
import copy
>>>
a = np.array([1, 'm', [2, 3, 4]], dtype = object) >>>
c = copy.deepcopy(a) >>>
c[2][0] = 10 >>>
c
array([1, 'm', list([10, 3, 4])], dtype = object) >>>
a
array([1, 'm', list([2, 3, 4])], dtype = object)