I have a text file which looks like:
12345
23451
11221
21219
11223
71231
I want to have an array of length (number_of_columns - 1) which will store the maximum values of each column respectively except for the last column.
So my output array for this above example will look something like : 7 3 4 5
I don't want to use pandas here. I am unable to understand how to proceed.
CodePudding user response:
Use:
with open(datafile) as infile:
# convert each line to an iterable of ints
rows = (map(int, line.strip()) for line in infile)
# find the maximum per col, exclude the last one
*res, _ = (max(col) for col in zip(*rows))
print(res)
Output
[7, 3, 4, 5]
As an alternative:
with open(datafile) as infile:
# convert each line to an iterable of ints exclude the last one
rows = (map(int, line.strip()[:-1]) for line in infile)
# find the maximum per col,
res = [max(col) for col in zip(*rows)]
print(res)
CodePudding user response:
[max(lst) for lst in zip(*[map(int,line) for line in data.split() if line])][:-1]
CodePudding user response:
Read the file:
with open("filename") as f:
text = f.read()
Collect the columns, and get the maximums:
from collections import defaultdict
cols = defaultdict(list)
for line in text.split("\n"):
for i, char in enumerate(line):
cols[i].append(char)
list(map(max, cols.values()))
>>> ['7', '3', '4', '5', '9']