在Python中用csv列写几个列表 [英] Write several list of lists in a csv columns in Python

查看:162
本文介绍了在Python中用csv列写几个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表的列表,需要写在csv列的每个元素。
使用下面的代码输出正是我想要的。

 在izip_longest(* averages_,fillvalue = [ '']):
writer.writerow(row [0] + row [1])$ ​​b $ b

行[0]的示例行:

  ['0.000000','0.000586','0.005819','0.011434 ','0.052012',0.000586118993] 

问题:
如何用一个带有变量的代码替换(row [0] + row [1])部分,以便它自动添加任意数量的子列表(row [i] + row [i + 1] + row [i + 2] ...)在主列表中?

替换(row [0] + row [1]) / p>

  list(itertools.chain.from_iterable(row))

  [子列表中项目的子项目] 

您是 flatte ning 清单列表。我选择了 https://stackoverflow.com/a/952952/2823755

I have a list of lists and need to write each of its elements in csv columns. Using the code below it outputs exactly what I want.

for row in izip_longest(*averages_, fillvalue = ['']):
    writer.writerow(row[0] + row[1])

a sample line of row[0]:

['0.000000', '0.000586', '0.005819', '0.011434', '0.052012', 0.000586118993]

Question: How can I replace the part (row[0] + row[1]) with a code with a variable in it so that it automatically adds any number of sub-lists (row[i] + row[i+1] + row[i+2]...) in the main list?

解决方案

If row is a list of lists, replace (row[0] + row[1]) with

list(itertools.chain.from_iterable(row))

or

[item for sublist in row for item in sublist]

You are flattening a lists of lists. I picked both of those from https://stackoverflow.com/a/952952/2823755

这篇关于在Python中用csv列写几个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆