Search code examples
pythonappenddata-analysis

How could I append a certain object from several lists into a new file?


I have hundreds of lists of numbers that contain different pressures, and I need to append each 5th object in each list into a new file. What the program is doing is going though hundreds of text files with thousands of lines of data in each. The program goes through each file in the path and separates the data into two lists: Mass_to_charge and Pressure. My code is:

import os

os.chdir('file_path')
path = os.listdir('file_path')
for file in path:
    MtoC = []
    Pressure = []
    readfile = open(file,'r')
    line = readfile.readline()
    line = readfile.readline()
    line = readfile.readline()
    line = readfile.readline()
    line = readfile.readline()
    line = readfile.readline()
    line = readfile.readline()
    line = readfile.readline()
    line = readfile.readline()
    line = readfile.readline()
    line = readfile.readline()
    line = readfile.readline()
    line = readfile.readline()
    line = readfile.readline()
    line = readfile.readline()
    line = readfile.readline()
    line = readfile.readline()
    line = readfile.readline()
    line = readfile.readline()
    line = readfile.readline()
    line = readfile.readline()
    line = readfile.readline()

    datalines = readfile.read().split("\n")
    useful_data = []

    for line in datalines:
        line = ''.join([x for x in line if x not in [",", "\r"]])
        data = [float(item) for item in line.split()]
        useful_data.append(data)
    combined_data = [MtoC.extend(sub_list) for sub_list in useful_data]
    Mass_to_charge = MtoC[::2]
    Pressure = MtoC[1::2]

If my next line under Pressure is print Pressure then it will print me out hundreds of lists of pressures, each with about 1000 objects. I need to append Pressure[4] of each list into a new file. I was thinking that something like:

for p in Pressure:
    file.append(p[4])

But this didn't work. I also was simply trying to print the 5th object of each Pressure list, but I couldn't figure that out either.


Solution

  • First of all use lowercase for variable names. Now, to take the 5th element of each list, from a list of lists, you can use map which applies a function to each item and returns the list of results:

    list_of_fifths = map(lambda x: x[4], Pressure)
    

    And example of running this:

    # create many lists...
    >>> list_of_lists = [range(i) for i in range(5, 15, 2)]
    >>> list_of_lists
    [[0, 1, 2, 3, 4], [0, 1, 2, 3, 4, 5, 6], [0, 1, 2, 3, 4, 5, 6, 7, 8], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]]
    # take fifth element of each
    >>> list_of_fifths = map(lambda x: x[4], list_of_lists)
    >>> list_of_fifths
    [4, 4, 4, 4, 4]