将项目附加到python中的列表列表[重复]

时间:2021-12-25 19:55:33

This question already has an answer here:

这个问题在这里已有答案:

I'm getting mad with list indexes, and can't explain what I'm doing wrong.

我对列表索引很生气,无法解释我做错了什么。

I have this piece of code in which I want to create a list of lists, each one containing values of the same circuit parameter (voltage, current etc..) that I'm reading from a csv file that looks like this:

我有这段代码,我想在其中创建一个列表列表,每个列表包含相同电路参数(电压,电流等)的值,我正在从一个csv文件中读取,如下所示:

Sample, V1, I1, V2, I2
0, 3, 0.01, 3, 0.02
1, 3, 0.01, 3, 0.03

And so on. What I want is to create a list that for example contains V1 and I1 (but I want to chose interactively) in the form [[V1], [I1]], so:

等等。我想要的是以[[V1],[I1]]的形式创建一个列表,例如包含V1和I1(但我想以交互方式选择),所以:

[[3,3], [0.01, 0.01]]

The code that I'm using is this:

我正在使用的代码是这样的:

plot_data = [[]]*len(positions)    
for row in reader:
    for place in range(len(positions)):
        value = float(row[positions[place]])
        plot_data[place].append(value)

plot_data is the list that contains all the values, while positions is a list with the indexes of the columns that I want to copy from the .csv file. The problem is that if I try the commands in the shell, seems to work, but if I run the script instead of appending each value to the proper sub-list, it appends all values to all lists, so I obtain 2 (or more) identical lists.

plot_data是包含所有值的列表,而position是一个列表,其中包含我要从.csv文件中复制的列的索引。问题是,如果我在shell中尝试命令,似乎工作,但如果我运行脚本而不是将每个值附加到正确的子列表,它会将所有值附加到所有列表,所以我获得2(或更多) )相同的清单。

2 个解决方案

#1


76  

Python lists are mutable objects and here:

Python列表是可变对象,在这里:

plot_data = [[]] * len(positions) 

you are repeating the same list len(positions) times.

你重复相同的列表len(位置)时间。

>>> plot_data = [[]] * 3
>>> plot_data
[[], [], []]
>>> plot_data[0].append(1)
>>> plot_data
[[1], [1], [1]]
>>> 

Each list in your list is a reference to the same object. You modify one, you see the modification in all of them.

列表中的每个列表都是对同一对象的引用。你修改一个,你看到所有这些修改。

If you want different lists, you can do this way:

如果您想要不同的列表,可以这样做:

plot_data = [[] for _ in positions]

for example:

例如:

>>> pd = [[] for _ in range(3)]
>>> pd
[[], [], []]
>>> pd[0].append(1)
>>> pd
[[1], [], []]

#2


2  

import csv
cols = [' V1', ' I1'] # define your columns here, check the spaces!
data = [[] for col in cols] # this creates a list of **different** lists, not a list of pointers to the same list like you did in [[]]*len(positions) 
with open('data.csv', 'r') as f:
    for rec in csv.DictReader(f):
        for l, col in zip(data, cols):
            l.append(float(rec[col]))
print data

# [[3.0, 3.0], [0.01, 0.01]]

#1


76  

Python lists are mutable objects and here:

Python列表是可变对象,在这里:

plot_data = [[]] * len(positions) 

you are repeating the same list len(positions) times.

你重复相同的列表len(位置)时间。

>>> plot_data = [[]] * 3
>>> plot_data
[[], [], []]
>>> plot_data[0].append(1)
>>> plot_data
[[1], [1], [1]]
>>> 

Each list in your list is a reference to the same object. You modify one, you see the modification in all of them.

列表中的每个列表都是对同一对象的引用。你修改一个,你看到所有这些修改。

If you want different lists, you can do this way:

如果您想要不同的列表,可以这样做:

plot_data = [[] for _ in positions]

for example:

例如:

>>> pd = [[] for _ in range(3)]
>>> pd
[[], [], []]
>>> pd[0].append(1)
>>> pd
[[1], [], []]

#2


2  

import csv
cols = [' V1', ' I1'] # define your columns here, check the spaces!
data = [[] for col in cols] # this creates a list of **different** lists, not a list of pointers to the same list like you did in [[]]*len(positions) 
with open('data.csv', 'r') as f:
    for rec in csv.DictReader(f):
        for l, col in zip(data, cols):
            l.append(float(rec[col]))
print data

# [[3.0, 3.0], [0.01, 0.01]]