2012-01-18 102 views
9

我得到了一些我无法理解的Python。 我有一个列表的列表,称为数据:列表副本不工作?

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]] 

我想创建的数据,其数据将被反转的副本(1变成99等)。

我那么有sorted_values和反向的:

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] 

创建数据的副本:

rev_data = data[:] 

然后,我改变的数据通过我的名单运行时:

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)] 

我的问题是,无论我的方法来创建rev_data,数据也被修改过程中!

我想:

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

rev_data = [] 
rev_data.extend(data) 

每一次,最后的答案是正确的,但数据也随之改变。 。 。

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]] 
+0

有几个问题问同样的,像这样的:http://stackoverflow.com/questions/6993531/复制列表中的蟒蛇和这一个:http://stackoverflow.com/questions/8350750/python-copy-list-issue – juliomalegria 2012-01-18 16:05:24

回答

19

data[:]创建列表data副本。由于这是一个列表的列表,你也想复制的内部列表,你需要一个副本,而不是:

rev_data = copy.deepcopy(data) 

rev_data = [x[:] for x in data] 
+0

男人,这么多答案在15秒钟! O_O。我知道它必须是嵌套列表,没有真正获得的东西。感谢大家! – jlengrand 2012-01-18 15:52:28

+2

值得注意的是,与这样的列表理解相比,'deepcopy'具有相当大的性能影响(通常是一个数量级)。所以,如果你能确定你的列表总是以同样的方式构建,那么使用第二种方法可能会更好。 – Wilduck 2012-01-18 16:13:42

6

您有一个嵌套列表的列表,因此您需要复制两个深度级别。

data_copy = [inner_list[:] for inner_list in data] 
7

使用copy.deepcopy复制嵌套的可变对象。

import copy 
rev_data = copy.deepcopy(data) 
.....................  

即:

>>> import copy 
>>> 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]] 
>>> 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] 
>>> 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] 
>>> for index, item in enumerate(rev_data): 
...  for s_index, s_item in enumerate(item): 
...   rev_data[index][s_index] = reverse_vals[sorted_vals.index(s_item)] 
...   
>>> 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]] 
>>> rev_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]] 
0

reverse_data = reversed(data)

reverse_data会一个迭代器。您可以遍历它,也可以强制它使用list(reverse_data)

更多关于内置功能:http://docs.python.org/library/functions.html#reversed - 将它放在枕头下。

编辑:如果你只是想有从100减去值的列表:

from operator import sub 
from functools import partial 
subtract_map = partial(map, partial(sub,100)) # this is more convenient than a nested list comprehension 
complements = [subtract_map(l) for l in data] 
+0

Downvoter:怎么了?不要相信你能轻松做到这一点? – Marcin 2012-01-18 16:01:12

+1

不知道原因:s。无论如何感谢提示;) – jlengrand 2012-01-18 16:07:31

+0

有趣的。我不知道'反转' – Benjamin 2012-01-18 16:07:40