Search code examples
pythonpython-2.7listtext-filesdatainputstream

Transposing data from automatically created lists


I am trying to transpose data from lists that are automatically created for each text file. Each text file gets its own version of the listsR list. I then place the lists into another list, listlist so that I can manage a list of lists. I know how to do this using declared lists but this code needs the flexibility to utilize any number of text files, transpose the lists and take the average of each index from among all the lists. This is hopefully being used to create baselines from the text files.

import os
import csv
import numpy as np

os.chdir('////Users////thomaswolff////Desktop////baseline2')

def listNew():
    listlist = []
    for data in os.listdir(os.getcwd()):
        if data.endswith('.TXT') and 'baseline' in data:
            with open(data,'rU') as file:
                listsR = [[] for i in xrange(0)]
                for row in csv.DictReader(file):
                    listsR.append(float(row[' IRI R e']))
                listlist.append(listsR)

This works fine for placing the data into lists of lists but i need to transpose the data according to the index. So listlist[0:][0] would be the first index from each list within listlist. Text files of the data I'm using can be found here at Github using the baseline.txt files:

As you can see from this code I've written in the past, I know how to do this using declared lists, but this is different.


Solution

  • To transpose listlist you can use zip()

    list_list_transpose = zip(*listlist)