0

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
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
Luke
  • 138
  • 2
  • 11
  • show how should look csv for a more deeper nesting, say 4 levels – RomanPerekhrest Feb 13 '23 at 21:02
  • 1
    If you don't know the number of lists, you should be using a *list* of lists, not individually numbered list variables. – chepner Feb 13 '23 at 21:02
  • @chepner Yes that's what I'm trying to do. The last example was just to show what I have tried and what didn't work for me – Luke Feb 13 '23 at 21:04
  • `zip(*list_of_lists)`? – chepner Feb 13 '23 at 21:04
  • One place to start is `zip(list_1, list_2)`. This will give you a list of tuples where each tuple contains a number and a list. Now you will need to transform the tuples into a single list. – Code-Apprentice Feb 13 '23 at 21:06
  • 1
    You have a list of data in column-major format. `csv.writerows` can write a row-major list-of-lists. You can do this by transposing your data. Does this answer your question? [Transpose list of lists](https://stackoverflow.com/questions/6473679/transpose-list-of-lists) (just do `col_data = [list_1, *list_2]` first) – Pranav Hosangadi Feb 13 '23 at 21:07
  • 2
    In each case pay attention to add __newline=''__ in order to avoid empty rows: with open(file_name, 'w', newline='') – Alexander Khomenko Feb 13 '23 at 21:59

2 Answers2

3

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)
Alexander Khomenko
  • 569
  • 1
  • 5
  • 13
1

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.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268