简体   繁体   English

在python中组合三个不同的列表

[英]Combining three different list in python

Thank you for looking at my issue.谢谢你看我的问题。

I'm trying to compare cells from three csv files to make sure they are exactly the same info.我正在尝试比较来自三个 csv 文件的单元格,以确保它们的信息完全相同。 the cells in the csv can contain names, dates or ID numbers. csv 中的单元格可以包含姓名、日期或 ID 号。 All have to match.都必须匹配。

compile = []
for a in Treader,Vreader,Dreader:
    for b in a:
        compile.append(b[0])

However, the number of variables will fluctuate and I don't want to keep adding index splicing every time.但是,变量的数量会波动,我不想每次都继续添加索引拼接。 see "complie.append(b[0])" .见 "complie.append(b[0])" 。 The question now what way can I construct this to give me a random amount of variables and random number of indexes based on the length "len" of the original list.现在的问题是我可以用什么方式构造它来根据原始列表的长度“len”为我提供随机数量的变量和随机数量的索引。 can i use the range function for that?我可以使用范围函数吗? not sure how i can create something like this.不知道我怎么能创造这样的东西。

The current question I have is我目前的问题是

List = [[sally,john,jim], [sally,john,jim], [sally,john,jim]]

If I have the list above how could I get it to show

List =[sally,sally,sally]
List1 = [john,john,john]
List2 = [jim,jim,jim]

Also I want to be able to come up with unlimited number of list based on the length of this list that is inside the list.此外,我希望能够根据列表中的列表长度提出无限数量的列表。 In this case its 3 for three names.在这种情况下,它的三个名称为 3。

Some of my list has 30 some has 5 so its important I can assign it without having to type list1 to list 30 and manually assign each one.我的某些列表有 30 个,有些有 5 个,所以它很重要,我可以分配它,而不必键入 list1 到列表 30 并手动分配每个。

you may use:你可以使用:

compile = list(zip(Treader,Vreader,Dreader))

this will create a list of tuples, a tuple will have like (sally,john,jim)这将创建一个元组列表,一个元组会像(sally,john,jim)


after your edit编辑后

you may use:你可以使用:

list(zip(*List))

output:输出:

[('sally', 'sally', 'sally'), ('john', 'john', 'john'), ('jim', 'jim', 'jim')]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM