列表副本不起作用? [英] List copy not working?

查看:71
本文介绍了列表副本不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Python有一些不了解的地方. 我有一个列表列表,称为数据:

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

我想创建一个数据副本,其数据将被反转(1变为99,依此类推).

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

然后我得到了sorted_values和相反的值:

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]

我创建数据副本:

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

我的问题是,无论我使用什么方法创建rev_data,在此过程中数据也会被修改!

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

我尝试过:

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

推荐答案

data[:]创建列表data副本.由于这是一个列表列表,并且您还想复制内部列表,因此需要一个 deep 副本:

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)

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

这篇关于列表副本不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆