user4792701
user4792701

Reputation:

Creating a new list for every line in a text file?

So I'm trying to create a program that will sort the contents of a text file in multiple ways (alphabetical, numerical order etc.). To do this I would need to create a new list for every item in a text file. Say my text file looked like this:

Isaac 2 5 3  
Aaron 9 8 10  

The output for one of the sorting methods (alphabetical, with only the highest score displayed) should look something like this:

Aaron 10  
Isaac 5  

To do this I would need to create a new list for every line in a text file, that way I could sort it multiple ways and I could remove any irrelevant info i.e lower scores. I have tried:

mylist = fileName.split("\n")  

and even:

mylist = fileName.readlines()  

But these just create a big list with a new item for every line, instead of an individual list for every line. Is there any way to do this with either these two methods or another method I haven't heard of?

Upvotes: 2

Views: 2496

Answers (3)

user4792701
user4792701

Reputation:

Okay guys so after experimenting with the answers provided I found a stupidly simple way to get what I needed:

for line in file.readlines():  
            items = line.split()  
            items = sorted(items, reverse = True)  
            del items[-2:]    
            print(items)  

Output looks like:

['Isaac', '5']  
['Aaron', '9']  

Upvotes: 0

rickhg12hs
rickhg12hs

Reputation: 11902

Using Pandas, here's one way to do it.

N.B.: I'm a Pandas noob.

Made a data file:

$ cat data.txt
Isaac 2 5 3
Aaron 9 8 10

and then used pandas with ipython:

In [3]: import pandas as pd

In [4]: df = pd.read_table('data.txt', delim_whitespace=True, header=None)

In [5]: df
Out[5]: 
       0  1  2   3
0  Isaac  2  5   3
1  Aaron  9  8  10

In [6]: newdf = df.sort(0).apply(lambda x: pd.Series([x[0],x[1:].max()]),axis=1)

In [7]: newdf
Out[7]: 
       0   1
1  Aaron  10
0  Isaac   5

In [8]: print(newdf.to_string(header=False, index=False))
 Aaron  10
 Isaac   5

In [9]: newdf.values
Out[9]: 
array([['Aaron', 10],
       ['Isaac', 5]], dtype=object)

In [10]: newdf.values.tolist()
Out[10]: [['Aaron', 10], ['Isaac', 5]]

Suggestions to simplify this would be greatly appreciated.

Upvotes: 0

Tim Pietzcker
Tim Pietzcker

Reputation: 336098

In a first iteration, assuming that f is the file object, you could simply do

mylist = [line.split() for line in f]

This gives you a list of lists like

[["Isaac", "2", "5", "3"], ["Aaron", "9", "8", "10"]]

However, if you want to sort numbers, you need to convert the strings to integers (or floats?) first, since "9" > "10". How to do this depends on the structure of your data.

Assuming the first element of your line is a name and everything else after that is integers, you can use

mylist = []
for line in f:
    items = line.split()
    mylist.append([items[0]] + [int(item) for item in items[1:]])

Result:

[['Isaac', 2, 5, 3], ['Aaron', 9, 8, 10]]

Upvotes: 3

Related Questions