Python: 将列表集合(1个嵌套列表)写入CSV文件

huangapple go评论58阅读模式
英文:

Python: write set of lists (1 nested list) to csv file

问题

以下是您提供的内容的翻译:

我有一组带有两个列表的数据。list_2 可以包含任意数量的嵌套列表(这就是我的问题所在)。

list_1 = [2000.0, 2100.0, 2200.0]
list_2 = [[-1.86, 0.49, 1.36], [-1.29, 0.59, 1.62], [-.99, 0.19, 1.76]]

在CSV文件中,它应该如下所示:

2000    -1.86    -1.29    -.99
2100    0.49    0.59     0.19
2200    1.36     1.62     1.76

我已经尝试了一些不同的写文件方法:

with open(file_name, 'w') as file:
    writer = csv.writer(file)
    writer.writerows(zip(list_1, list_2))

我还尝试在这个问题中看到的先将它们压缩在一起。

我还尝试分离列表(在 list_2 中),但这不允许增长。在运行应用程序之前,我不知道嵌套列表的数量,所以它必须能够增长。

writer.writerows(zip(list_1, list_2, list_3, list_4))  # 不允许动态增长
英文:

I have a set of data with two lists. list_2 can have any number of nested lists in it (which is where my problem stems from).

list_1 = [2000.0, 2100.0, 2200.0]
list_2 = [[-1.86, 0.49, 1.36], [-1.29, 0.59, 1.62], [-.99, 0.19, 1.76]]

once in the csv file it should look like

2000    -1.86    -1.29    -.99
2100    0.49    0.59     0.19
2200    1.36     1.62     1.76

I have tried a few different ways of writing the file

    with open(file_name, 'w') as file:
        writer = csv.writer(file)
        writer.writerows(zip(list_1, list_2))

I've also tried zipping them before as see in this question

I've also tried separating the lists (in list_2) but that does not allow for any growth. I do not know the number of nested lists before running the application so it has to be able to grow.

writer.writerows(zip(list_1, list_2, list_3, list_4))  # does not allow for dynamic growth

答案1

得分: 3

如果我理解正确,您不知道将有多少个列表。但它们都将具有相同的大小?

而且您希望它们以"垂直"方式存储?

我有一种感觉,transpose 操作将对您非常有帮助。

import csv
import numpy

list_1 = [2000.0, 2100.0, 2200.0]
list_2 = [[-1.86, 0.49, 1.36], [-1.29, 0.59, 1.62], [-0.99, 0.19, 1.76]]

list_3 = [list_1] + list_2
output_array = numpy.transpose(numpy.array(list_3))

with open(file_name, 'w', newline='') as file:
    writer = csv.writer(file)
    writer.writerows(output_array)
英文:

if I get you right you don't know how many lists you are going to have. But all of them will have the same size?

And you want them to be stored "vertically"?

I have the feeling a transpose
operation would help you very well.

import csv
import numpy

list_1 = [2000.0, 2100.0, 2200.0]
list_2 = [[-1.86, 0.49, 1.36], [-1.29, 0.59, 1.62], [-.99, 0.19, 1.76]]

list_3 = [list_1] + list_2
output_array = numpy.transpose(numpy.array(list_3))

with open(file_name, 'w', newline='') as file:
    writer = csv.writer(file)
    writer.writerows(output_array)

答案2

得分: 1

你可以使用*操作符来实现这个:

zip(list_1, *list_2)

这将创建一个包含多个子列表的列表,其中每个子列表的第一个元素来自list_1,而其余元素来自list_2中的各个列表。

英文:

You can do this with the * operator:

zip(list_1, *list_2)

This will create a list of lists where the first element of each sub list comes from list_1 and the remaining elements come from each of the lists in list_2.

huangapple
  • 本文由 发表于 2023年2月14日 04:53:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75441086.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定