[英]How to create multiple (but individual) empty lists in Python?
I wrote a script that at one point generates a bunch of empty lists applying a code with the structure: 我写了一个脚本,在某一点上生成一堆空列表,应用具有以下结构的代码:
A,B,C,D=[],[],[],[]
which produces the output: 产生输出:
A=[]
B=[]
C=[]
D=[]
The way it is right now, I have to manually modify the letters each time I use a different dataset as an input. 它现在的方式,每次我使用不同的数据集作为输入时,我必须手动修改字母。 I want to be able to automatize that.
我希望能够实现自动化。 I thought about doing this:
我想过这样做:
FieldList=[A,B,C,D]
bracket=[]
[[0]for field in FieldList]
for field in FieldList:
bracket=bracket+["[]"]
FieldList=bracket
Here I was trying to replicate " A,B,C,D=[],[],[],[]", but evidently that's not how it works. 在这里,我试图复制“A,B,C,D = [],[],[],[]”,但显然这不是它的工作原理。
I also tried: 我也尝试过:
FieldList=[A,B,C,D]
bracket=[]
[[0]for field in FieldList]
for field in FieldList:
field=[]
But at the end it just produces a single list call "field". 但最后它只产生一个列表调用“字段”。
So, this is what I need the lists for. 所以,这就是我需要的列表。 I will be reading information from a csv and adding the data I'm extracting from each row to the lists.
我将从csv中读取信息,并将我从每行中提取的数据添加到列表中。 If generate a "list of lists", can I still call each of them individually to append stuff to them?
如果生成“列表列表”,我还可以单独调用它们中的每一个来附加内容吗?
A,B,C,D=[],[],[],[] with open(csvPath+TheFile, 'rb') as csvfile: #Open the csv table r = csv.reader(csvfile, delimiter=';') #Create an iterator with all the rows of the csv file, indicating the delimiter between columns for i,row in enumerate(r): #For each row in the csv if i > 0: #Skip header A.append(float(row[0])) #Extract the information and append it to each corresponding list B.append(float(row[1])) C.append(format(row[2])) D.append(format(row[3]))
You are overcomplicating things. 你太复杂了。 Just use a list or dictionary:
只需使用列表或字典:
fields = {key: [] for key in 'ABCD'}
then refer to fields['A']
, etc. as needed, or loop over the structure to process each in turn. 然后根据需要参考
fields['A']
等,或循环结构以依次处理每个fields['A']
。
dict((k, []) for k in ['A','B','C','D'])
Based on your usage example, what you actually want is zip()
: 根据您的用法示例,您真正想要的是
zip()
:
For this example, note that csv.reader()
basically breaks the file up into data of the form: 对于此示例,请注意
csv.reader()
基本上将文件分解为表单的数据:
[
["a1", "b1", "c1", "d1"],
["a2", "b2", "c2", "d2"],
...,
["an", "bn", "cn", "dn"]
]
The Example: 这个例子:
table = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
]
#How to transpose the data into columns??? Easy!
columns = zip(*table)
Now, you have a variable, columns
, of the form: 现在,您有一个表格的变量
columns
:
columns = [
[1, 5, 9],
[2, 6, 10],
[3, 7, 11],
[4, 8, 12]
]
Okay so let's apply this to a csv file: 好吧,让我们将它应用于csv文件:
with open("mycsv.csv", "rb") as infile:
reader = csv.reader(infile, delimiter=";")
next(reader, None) #skip the header
columns = zip(*reader)
That's it! 而已!
Note: For this example, we are assuming that "mycsv.csv"
has the correct number of columns in every row. 注意:对于此示例,我们假设
"mycsv.csv"
在每行中具有正确的列数。 You may need to implement a check to make sure that all of the rows are "filled in". 您可能需要执行检查以确保所有行都“填写”。
Check the accepted answer here . 在这里检查接受的答案。 (Answer by To Click or Not to Click)
(通过点击或不点击回答)
>>> obj = {}
>>> for i in range(1, 21):
... obj['l'+str(i)] = []
...
>>>obj
{'l18': [], 'l19': [], 'l20': [], 'l14': [], 'l15': [], 'l16': [], 'l17': [], 'l10': [], 'l11': [], 'l12': [], 'l13': [], 'l6': [], 'l7': [], 'l4': [], 'l5': [], 'l2': [], 'l3': [], 'l1': [], 'l8': [], 'l9': []}
>>>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.