如何使用python选择特定列表元素并将其添加到csv行

如何使用python选择特定列表元素并将其添加到csv行,python,list,loops,csv,Python,List,Loops,Csv,我得到了2个列表,其中包含6个元素和一个循环,用于将行写入csv文件。例如: list1 = [1, 2, 3, 4, 5, 6] list2 = [a, b, c, d, e, f] for i in range(0, 6): writer.writerow([stuff1, stuff2, list_element1, list_element2]) 我想选择列表1和2的第一个元素并将它们添加到行的末尾,然后我想选择列表1和2的第二个元素并将它们添加到csv文件[…]的第二行 提

我得到了2个列表,其中包含6个元素和一个循环,用于将行写入csv文件。例如:

list1 = [1, 2, 3, 4, 5, 6]
list2 = [a, b, c, d, e, f]

for i in range(0, 6):
    writer.writerow([stuff1, stuff2, list_element1, list_element2])
我想选择列表1和2的第一个元素并将它们添加到行的末尾,然后我想选择列表1和2的第二个元素并将它们添加到csv文件[…]的第二行


提前感谢。

假设list1和list2的长度相同,使用zip(list1,list2)可能看起来有点像pythonic,但它增加了空间复杂性,并增加了生成元组的时间,因为
zip
不会创建生成器而不是元组。因此,尽管下面的外观明显且简单,但它也是最佳的

list1 = [1, 2, 3, 4, 5, 6]
list2 = [a, b, c, d, e, f]

for i in xrange(len(list1)):
    writer.writerow([stuff1, stuff2, list1[i], list2[i])

假设list1和list2的长度相同,使用zip(list1,list2)可能看起来有点像pythonic,但它增加了空间复杂性,并且增加了生成元组的时间,因为
zip
不会创建生成器,而是创建元组。因此,尽管下面的外观明显且简单,但它也是最佳的

list1 = [1, 2, 3, 4, 5, 6]
list2 = [a, b, c, d, e, f]

for i in xrange(len(list1)):
    writer.writerow([stuff1, stuff2, list1[i], list2[i])

有不同的方法,都取决于您迭代的对象类型和运行时注意事项。对于这个简单的例子,任何事情都应该是好的,但是要考虑每一个利弊。

list1 = [1, 2, 3, 4, 5, 6]
list2 = [a, b, c, d, e, f]

# way 1
for l1, l2 in zip(l1, l2):
     writer.writerow([stuff1, stuff2, l1, l2])

from itertools import izip, izip_longest

# way 2: for equal lengths, better if iterable don't support __len__
# for e.g. generators not having len
for i, _ in enumerate(list1):
    print i
    writer.writerow([stuff1, stuff2, list1[i], list2[i])

# way 3: for equal lengths
# using len() would break if iterable don't support it.
for i in range(len(list1)):
    writer.writerow([stuff1, stuff2, list1[i], list2[i]])

# way 4: for equal lengths - better for huge lists
for i in xrange(len(list1)):
    writer.writerow([stuff1, stuff2, list1[i], list2[i]])

# way 5: izip - equal lengths
list1 = [1, 2, 3, 4, 5, 6]
list2 = [7, 8, 9 ,10, 11, 12]
for l1, l2 in izip(list1, list2):
    writer.writerow([stuff1, stuff2, l1, l2])

# way 6: for different lengths
list1 = [1, 2, 3, 4, 5, 6]
list2 = [7, 8, 9 ,10, 11, 12, 13]
for l1, l2 in izip_longest(list1, list2):
    writer.writerow([stuff1, stuff2, l1, l2])

# way 7: for different lengths
for l1, l2 in map(None, list1, list2):
    writer.writerow([stuff1, stuff2, l1, l2])

有不同的方法,都取决于您迭代的对象类型和运行时注意事项。对于这个简单的例子,任何事情都应该是好的,但是要考虑每一个利弊。

list1 = [1, 2, 3, 4, 5, 6]
list2 = [a, b, c, d, e, f]

# way 1
for l1, l2 in zip(l1, l2):
     writer.writerow([stuff1, stuff2, l1, l2])

from itertools import izip, izip_longest

# way 2: for equal lengths, better if iterable don't support __len__
# for e.g. generators not having len
for i, _ in enumerate(list1):
    print i
    writer.writerow([stuff1, stuff2, list1[i], list2[i])

# way 3: for equal lengths
# using len() would break if iterable don't support it.
for i in range(len(list1)):
    writer.writerow([stuff1, stuff2, list1[i], list2[i]])

# way 4: for equal lengths - better for huge lists
for i in xrange(len(list1)):
    writer.writerow([stuff1, stuff2, list1[i], list2[i]])

# way 5: izip - equal lengths
list1 = [1, 2, 3, 4, 5, 6]
list2 = [7, 8, 9 ,10, 11, 12]
for l1, l2 in izip(list1, list2):
    writer.writerow([stuff1, stuff2, l1, l2])

# way 6: for different lengths
list1 = [1, 2, 3, 4, 5, 6]
list2 = [7, 8, 9 ,10, 11, 12, 13]
for l1, l2 in izip_longest(list1, list2):
    writer.writerow([stuff1, stuff2, l1, l2])

# way 7: for different lengths
for l1, l2 in map(None, list1, list2):
    writer.writerow([stuff1, stuff2, l1, l2])

是否要将行添加到已存在的csv文件?是否要将行添加到已存在的csv文件?在这种情况下,最好使用
zip
。它更简洁。此外,考虑如果列表的长度要改变或变得不均匀-你很可能会结束丢失的元素或者结束< <代码>索引错误>代码>我同意zip看起来是Python的。在这里,zip创建了一个本文档中不需要的附加元组scenario@Tagc使用zip时,空间和时间的复杂性很高。只需对大小为10000000的列表1和列表2进行相同的尝试,并检查时间和内存消耗情况
zip
在这种情况下更适合使用。它更简洁。此外,考虑如果列表的长度要改变或变得不均匀-你很可能会结束丢失的元素或者结束< <代码>索引错误>代码>我同意zip看起来是Python的。在这里,zip创建了一个本文档中不需要的附加元组scenario@Tagc使用zip的空间和时间复杂度很高。只需对大小为10000000的列表1和列表2进行相同的尝试,并检查时间和内存消耗