如何在Python上将列表的所有值导出为csv 输出 预期产量

如何在Python上将列表的所有值导出为csv 输出 预期产量,python,python-3.x,list,csv,Python,Python 3.x,List,Csv,我有3个列表,list_1有8个项目,list_2有10个项目,list_3也有10个项目 但是当我将列表写入csv时,list_2和list_3列不显示最后两项。这是zip的默认行为:截断为最短iterable的长度。您可以改为使用: 首先导入它: 然后将分配cols的行替换为: 你可以看到这个和这个 使用时传递长度不等的参数 Pythonzip()函数,注意长度很重要 你的平民百姓。有可能你传来的那些犹太人 参数的长度不同 在这些情况下,zip()输出的元素数量将是 等于最短iter

我有3个列表,
list_1
有8个项目,
list_2
有10个项目,
list_3
也有10个项目


但是当我将列表写入csv时,
list_2
list_3
列不显示最后两项。

这是
zip
的默认行为:截断为最短iterable的长度。您可以改为使用:

  • 首先导入它:
  • 然后将分配
    cols
    的行替换为:
你可以看到这个和这个

使用时传递长度不等的参数 Python
zip()
函数,注意长度很重要 你的平民百姓。有可能你传来的那些犹太人 参数的长度不同

在这些情况下,
zip()
输出的元素数量将是 等于最短iterable的长度剩余元素 在任何时候,iterables都将被
zip()

在您的情况下,您将被int剪切到第8个值(排序列表)

编辑

您可以使用此信息

itertools.zip_longest(*iterables[,fillvalue])

制作一个迭代器,聚合来自每个ITerable的元素。 如果iterables的长度不均匀,则会填充缺少的值 具有填充值。迭代将继续,直到找到最长的iterable 筋疲力尽的。相当于:

如果其中一个iterables可能是无限的,那么 zip_longest()函数应该用限制 调用数(例如islice()或takewhile())。如果不是 指定时,fillvalue默认为无

例如:

def zip_longest(*args, fillvalue=None):
     # zip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D-
     def sentinel(counter = ([fillvalue]*(len(args)-1)).pop):
         yield counter()         # yields the fillvalue, or raises IndexError
     fillers = repeat(fillvalue)
     iters = [chain(it, sentinel(), fillers) for it in args]
     try:
         for tup in zip(*iters):
            yield tup
     except IndexError:
         pass

非常感谢你的帮助。谢谢你的解释。
list1   list2   list3
  1      n1      o1
  2      n2      o2
  3      n3      o3
  4      n4      o4
  5      n5      o5
  6      n6      o6
  7      n7      o7
  8      n8      o8
list1   list2   list3
  1      n1      o1
  2      n2      o2
  3      n3      o3
  4      n4      o4
  5      n5      o5
  6      n6      o6
  7      n7      o7
  8      n8      o8
         n9      o9
         n10     o10 
from itertools import zip_longest
cols = zip_longest(list_1,list_2,list_3, fillvalue="")
def zip_longest(*args, fillvalue=None):
     # zip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D-
     def sentinel(counter = ([fillvalue]*(len(args)-1)).pop):
         yield counter()         # yields the fillvalue, or raises IndexError
     fillers = repeat(fillvalue)
     iters = [chain(it, sentinel(), fillers) for it in args]
     try:
         for tup in zip(*iters):
            yield tup
     except IndexError:
         pass
from itertools import zip_longest

l_1 = [1, 2, 3]
l_2 = [1, 2]

combinated = list(zip_longest(l_1, l_2, fillvalue="_"))

print(combinated)  # [(1, 1), (2, 2), (3, '_')]