Reputation: 13
I have Datalog file in the format
41, 3.68, 3.58, 3.71, 3.54, 3.68, 3.79
42, 3.69, 3.57, 3.73, 3.55, 3.67, 3.78
43, 3.68, 3.57, 3.73, 3.54, 3.68, 3.79
44, 3.68, 3.57, 3.73, 3.54, 3.67, 3.79
45, 3.68, 3.57, 3.73, 3.54, 3.67, 3.78
46, 3.68, 3.57, 3.73, 3.54, 3.67, 3.78
47, 3.68, 3.57, 3.73, 3.54, 3.67, 3.78
48, 3.68, 3.57, 3.73, 3.54, 3.67, 3.78
49, 3.68, 3.57, 3.73, 3.54, 3.67, 3.78
For each column I am trying to find the minimum value and the corresponding row number.
But with the below code the min()
function gives the 'float object not iterable error.
with open("Datalog.txt", "r") as file:
result = [[float(x) for x in line.split(",")] for line in file]
for i in range (len(result[0])):
for j in range (len(result)):
print (result[j][i])
print (min(result[0][i]))
Upvotes: 0
Views: 7144
Reputation: 3067
min iterates through the list/tuple in the argument and gives you the smallest item in it, but you're providing it with a float, so it's trying to iterate through the float.
There's absolutely no reason to do this, but it's not gonna crash if you do:
print (min(result[0][i],))
since now it's not a float, but a tuple. It will just print the value of result[0][i]
If what you want is to get the minimum value in each line, you gotta do:
for i in range (len(result)):
print (min(result[i]))
If what you want is the minimum in each column (assuming all rows have the same number of columns), you gotta do:
for j in range(len(result[0]))
print ('minimum in column ' + str(j) + ':')
print (min([result[i][j] for i in range(len(result))]))
where j is the index of the column
Upvotes: 1
Reputation: 122493
The error is because in
print (min(result[0][i]))
result[0][i]
is a single float
object, not a list as you expected.
Instead, give a list to min
:
for j in range (len(result)):
print(result[j][0])
print(min(result[j][1:]))
The range [1:]
means everything except the first element.
Upvotes: 1