Home > Software design >  Modifying input data to a usable list format using python
Modifying input data to a usable list format using python

Time:01-22

I have input data in the following format stored in a text file

Input Data

I am trying to create separate lists with every column as a list with last element of the column as the first element as:

list1 = [D, T, W, F, J, S, H, N] . . list3 = [L, Q, V]

Tried reading contents and then splitting the list contents but end up losing the index as the spaces are ignored. How to approach it?

CodePudding user response:

Here is one way to create the lists dynamically based on your (.txt) file/dataframe :

import pandas as pd

_ = (
       pd.read_fwf("/tmp/file.txt" header=None)
            .set_axis(list(df.iloc[-1]), axis=1)
            .iloc[::-1][1:]
            .pipe(lambda df: [exec(f"globals()['list{k}'] = ([e.strip('[]') \
                                               for e in v if str(e) != 'nan'])")
                              for k,v in df.to_dict("list").items()])
    )

Output :

print([(var, val) for var, val in globals().items() if var.startswith("list")])

[('list1', ['D', 'T', 'W', 'F', 'J', 'S', 'H', 'N']),
 ('list2', ['H', 'R', 'P', 'Q', 'T', 'N', 'B', 'G']),
 ('list3', ['L', 'Q', 'V']),
 ('list4', ['N', 'B', 'S', 'W', 'R', 'Q']),
 ('list5', ['N', 'D', 'F', 'T', 'V', 'M', 'B']),
 ('list6', ['M', 'D', 'B', 'V', 'H', 'T', 'R']),
 ('list7', ['D', 'B', 'Q', 'J']),
 ('list8', ['D', 'N', 'J', 'V', 'R', 'Z', 'H', 'Q']),
 ('list9', ['B', 'N', 'H', 'M', 'S'])]

CodePudding user response:

Use read_fwf for read file first and then in list comprehension create list of lists:

df = pd.read_fwf(file, header=None)

L =  [v[1:].str.strip('[]').dropna().tolist() 
                   for k, v in df.iloc[::-1].to_dict('series').items()]

print (L)

[['D', 'T', 'W', 'F', 'J', 'S', 'H', 'N'],
 ['H', 'R', 'P', 'Q', 'T', 'N', 'B', 'G'],
 ['L', 'Q', 'V'],
 ['N', 'B', 'S', 'W', 'R', 'Q'],
 ['N', 'D', 'F', 'T', 'V', 'M', 'B'], 
 ['M', 'D', 'B', 'V', 'H', 'T', 'R'], 
 ['D', 'B', 'Q', 'J'], 
 ['D', 'N', 'J', 'V', 'R', 'Z', 'H', 'Q'],
 ['B', 'N', 'H', 'M', 'S']]
  • Related