I have a text file that looks something like this:
hello 12
hello 56
world 25
world 26
Is there a way in python that I can somehow parse the list that I obtain from reading this data to obtain two separate nested lists (or a numpy array)? One for the first two rows containing hello and the other for the next two rows containing world. I basically need to get a separate nested list for something unique in the first column (so "hello" and "world" are not previously known).
Use a dict and group by the first column:
from csv import reader
from collections import defaultdict
with open("in.txt") as f:d = defaultdict(list)for k, v in reader(f,delimiter=" "):d[k].append(v)print(d.values())
Which will give you all the values in two separate lists:
[['25', '26'], ['12', '56']
If the data is always in two sections you can use a groupby:
from itertools import groupby
from csv import reader
from operator import itemgetterwith open("in.txt") as f:print([list(map(itemgetter(1), v)) for k, v in groupby(reader(f,delimiter=" "), key=itemgetter(0))])
Which will give the same output:
[['12', '56'], ['25', '26']]