Write Several List Of Lists In A Csv Columns In Python
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 = [
Solution 1:
If row is a list of lists, replace (row[0] + row[1])
with
list(itertools.chain.from_iterable(row))
or
[item forsublistin row foritemin sublist]
You are flattening a lists of lists. I picked both of those from https://stackoverflow.com/a/952952/2823755
Solution 2:
If I understand well what you want than the following should do the job :
writer.writerow(reduce(lambda x, y: x+y, row))
This work over all elements of row. If you want to sum the first count elements you can try like that :
writer.writerow(reduce(lambda x, y: x+y, row[:count - 1]))
Post a Comment for "Write Several List Of Lists In A Csv Columns In Python"