Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List copy not working? [duplicate]

Tags:

python

list

I got something with Python that I can't understand. I have a list of lists, called data:

data = [[75], [95, 64], [17, 47, 82], [18, 35, 87, 10], [20, 4, 82, 47, 65], [19, 1, 23, 75, 3, 34], [88, 2, 77, 73, 7, 63, 67], [99, 65, 4, 28, 6, 16, 70, 92], [41, 41, 26, 56, 83, 40, 80, 70, 33], [41, 48, 72, 33, 47, 32, 37, 16, 94, 29], [53, 71, 44, 65, 25, 43, 91, 52, 97, 51, 14], [70, 11, 33, 28, 77, 73, 17, 78, 39, 68, 17, 57], [91, 71, 52, 38, 17, 14, 91, 43, 58, 50, 27, 29, 48], [63, 66, 4, 68, 89, 53, 67, 30, 73, 16, 69, 87, 40, 31], [4, 62, 98, 27, 23, 9, 70, 98, 73, 93, 38, 53, 60, 4, 23]]

I want to create a copy of data, whose data would be reversed (1 becomes 99 and so on).

I then have the sorted_values and the reverse ones:

sorted_vals = [1, 2, 3, 4, 6, 7, 9, 10, 11, 14, 16, 17, 18, 19, 20, 23, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 38, 39, 40, 41, 43, 44, 47, 48, 50, 51, 52, 53, 56, 57, 58, 60, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 75, 77, 78, 80, 82, 83, 87, 88, 89, 91, 92, 93, 94, 95, 97, 98, 99]
reverse_vals = [99, 98, 97, 95, 94, 93, 92, 91, 89, 88, 87, 83, 82, 80, 78, 77, 75, 73, 72, 71, 70, 69, 68, 67, 66, 65, 64, 63, 62, 60, 58, 57, 56, 53, 52, 51, 50, 48, 47, 44, 43, 41, 40, 39, 38, 37, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 23, 20, 19, 18, 17, 16, 14, 11, 10, 9, 7, 6, 4, 3, 2, 1]

I create a copy of data:

rev_data = data[:] 

Then, I change data while running through my list:

for index, item in enumerate(rev_data):
    for s_index, s_item in enumerate(item):
        rev_data[index][s_index] = reverse_val[sort_val.index(s_item)]

My problem is that whatever my method to create rev_data, data is also modified during the process!

I tried:

rev_data = list(data)
rev_data = data[:]

rev_data = []
rev_data.extend(data)

Each time, the final answer is correct, but data is also changed . . .

data = [[25], [4, 35], [83, 52, 18], [82, 64, 16, 91], [78, 95, 18, 52, 34], [80, 99, 77, 25, 97, 65], [14, 98, 23, 26, 93, 37, 32], [1, 34, 95, 71, 94, 87, 29, 9], [57, 57, 73, 43, 17, 58, 19, 29, 66], [57, 51, 27, 66, 52, 67, 63, 87, 6, 70], [44, 28, 53, 34, 75, 56, 10, 47, 3, 48, 88], [29, 89, 66, 71, 23, 26, 83, 20, 60, 31, 83, 41], [10, 28, 47, 62, 83, 88, 10, 56, 40, 50, 72, 70, 51], [37, 33, 95, 31, 11, 44, 32, 69, 26, 87, 30, 16, 58, 68], [95, 38, 2, 72, 77, 92, 29, 2, 26, 7, 62, 44, 39, 95, 77]]
like image 689
jlengrand Avatar asked Jan 18 '12 15:01

jlengrand


People also ask

Why is copy () not working in Python?

Python List Copy Not Working. The main reason why the list. copy() method may not work for you is because you assume that it creates a deep copy when, in reality, it only creates a shallow copy of the list.

Does list () make a copy?

Using built-in method list() to copy list. Another way to copy a list in Python is to use the built-in method list(). It also creates a shallow copy which means any time a modification is made in the new list, it will not show on the old list.

How do you replicate a list?

The replication of list of a list can be created by using rep function. For example, if we have a list called x and we want to create five times replicated list of this list then we can use the code rep(list(x),5).

Can you duplicate a list in Python?

Using the extend() methodThe lists can be copied into a new list by using the extend() function. This appends each element of the iterable object (e.g., another list) to the end of the new list.


1 Answers

data[:] creates a shallow copy of the list data. Since this is a list of lists, and you also want to copy the inner lists, you need a deep copy instead:

rev_data = copy.deepcopy(data)

or

rev_data = [x[:] for x in data]
like image 197
Sven Marnach Avatar answered Oct 14 '22 17:10

Sven Marnach