Python无法正确将列表添加到列表中
我有一个名为divider.csv的文件,里面的内容如下:
divider.csv的内容下面的程序将这个文件的内容复制到一个叫'total'的变量里。
import csv
total = list()
temp_list_helper = list()
with open('divider.csv') as file_obj:
reader_obj = csv.reader(file_obj)
for row in reader_obj:
for a in row:
temp_list_helper.append(int(a))
total.append(temp_list_helper)
print(total)
temp_list_helper.clear()
输出结果如下:
[[-16963, -135, -165, -855, 484, 442, -107, -815, 1302, -1349, -1530, 1016, -351, 98, -188, 1234, 780, -812, -679, 4326, -7363, -2842, -3012, 1583, -3817, -4370, -3722, 4775, 3254, 2372, -8930, 1212, 2204, -5689, 1849, 1590, -4452, 3462, -8435, 4024, -189, 5059, 2255, 873, -1529, -3092, 906, 5791, 1904, 2190, -7520]]
[[29845, -28, 465, -195, -1123, 1669, 1755, 857, -3489, -3400, -542, -1009, 571, -1179, 3976, -3333, -2647, 5000, 3339, 6181, 1886, -6547, -2860, -2778, 3960, 2177, -4167, -3380, -1829, 3590, -1535, 5089, 1242, -338, 1826, -5699, -224, 194, 3144, 5578, 224, 3635, -4730, -5305, -1057, 2664, 1373, 925, 1166, 2916, 21837], [29845, -28, 465, -195, -1123, 1669, 1755, 857, -3489, -3400, -542, -1009, 571, -1179, 3976, -3333, -2647, 5000, 3339, 6181, 1886, -6547, -2860, -2778, 3960, 2177, -4167, -3380, -1829, 3590, -1535, 5089, 1242, -338, 1826, -5699, -224, 194, 3144, 5578, 224, 3635, -4730, -5305, -1057, 2664, 1373, 925, 1166, 2916, 21837]]
[[3231, -273, -81, 211, 227, 1091, 906, -1023, 279, -111, 1072, 570, 1261, 759, -1927, 2958, 138, 905, 2467, 141, -1711, -5792, -1007, -2484, 326, -6297, -5845, 94, -1109, -3426, 2907, -1987, -830, 2798, -549, 7207, -339, -548, -2081, -1711, 789, -996, -2054, 6114, 2429, -6915, 7946, 4191, 1434, 6793, -3686], [3231, -273, -81, 211, 227, 1091, 906, -1023, 279, -111, 1072, 570, 1261, 759, -1927, 2958, 138, 905, 2467, 141, -1711, -5792, -1007, -2484, 326, -6297, -5845, 94, -1109, -3426, 2907, -1987, -830, 2798, -549, 7207, -339, -548, -2081, -1711, 789, -996, -2054, 6114, 2429, -6915, 7946, 4191, 1434, 6793, -3686], [3231, -273, -81, 211, 227, 1091, 906, -1023, 279, -111, 1072, 570, 1261, 759, -1927, 2958, 138, 905, 2467, 141, -1711, -5792, -1007, -2484, 326, -6297, -5845, 94, -1109, -3426, 2907, -1987, -830, 2798, -549, 7207, -339, -548, -2081, -1711, 789, -996, -2054, 6114, 2429, -6915, 7946, 4191, 1434, 6793, -3686]]
从上面的结果可以看到,最开始的值没有被保存,而是被新的值覆盖了。这里面有什么错误呢?解决办法是什么呢?
3 个回答
0
我认为这个问题可以通过在每次循环开始时把temp_list_helper列表设为空列表来解决。可以用下面的代码实现:
import csv
total = list()
with open('divider.csv') as file_obj:
reader_obj = csv.reader(file_obj)
for row in reader_obj:
temp_list_helper = []
for a in row:
temp_list_helper.append(int(a))
total.append(temp_list_helper)
print(total)
0
当你添加 temp_list_helper
时,其实是把一个引用添加到了那个特定的列表里。
如果你清空 temp_list_helper
,那么你也会清空这个引用(这意味着在 total
里面插入的那个列表也会被清空)。
你应该在每次循环的时候重新创建 temp_list_helper
。
只需要把 temp_list_helper.clear()
改成 temp_list_helper = []
,或者在循环开始的时候添加它:
for row in reader_obj:
temp_list_helper = [] # clear the list
for a in row:
...
3
你在每一行中都在使用同一个列表实例,这样会导致问题。你需要在每次循环时都创建一个新的列表:
import csv
total = []
with open('divider.csv') as file_obj:
reader_obj = csv.reader(file_obj)
for row in reader_obj:
temp_list_helper = []
for a in row:
temp_list_helper.append(int(a))
total.append(temp_list_helper)
print(total)