Reputation: 425
I have to following list:
[[(640, 92, 0.80969274, 0)], [(524, 180, 0.58553714, 1)], [(494, 181, 0.64085102, 2)], [(477, 338, 0.67508131, 3)], [(554, 480, 0.64356369, 4)]]
I'm interested only in the first 2 elements in the items, for example:
640, 92
524, 180
494, 181
I can get them using
print list[1][0][0:2]
But what I want to do is to take max and mean by column. First I want to take the max and min for the first column, and then max and min from the second column.
in matlab we can do something like:
max(list(:,1));
min(list(:,1));
max(list(:,2));
min(list(:,2)));
Is there a way to do this in python without any headache?
Thank you!
Upvotes: 2
Views: 284
Reputation: 96350
As the other answers have pointed out, fundamentally, you are not using a good data structure for your task. If you want to similar functionality to Matlab in Python, you need to use numpy
and should take a look at pandas
. Python is a general purpose programming language, so its sequence types like list
are not really meant to efficiently do the same things as a Matlab mutli-dimensional array (which is what numpy
gives you). But that being said, there is a relatively painless way of doing what you want. It would be made even more painless if instead of using a list of length 1 lists, each containing a tuple that actually holds your data, you just use a list of tuples. But, python makes this sort of munging pretty painless:
>>> x = [[(640, 92, 0.80969274, 0)], [(524, 180, 0.58553714, 1)], [(494, 181, 0.64085102, 2)], [(477, 338, 0.67508131, 3)], [(554, 480, 0.64356369, 4)]]
>>> extracted = [sub[0:2] for l in x for sub in l]
In the list comprehension above, I just iterate over the list, then iterate over the (length one) list wrapping the tuple, then slice the columns I want from the tuple (sub
).
Now, to do a "transpose" in vanilla python, unpack into zip
:
>>> for col in zip(*extracted):
... print("Max: {}".format(max(col)))
... print("Min: {}".format(min(col)))
...
Max: 640
Min: 477
Max: 480
Min: 92
>>>
By the way, using Python 3.6 makes things even more painless by giving us f-strings!:
>>> for col in zip(*extracted):
... print(f"Max: {max(col)}")
... print(f"Min: {min(col)}")
...
Max: 640
Min: 477
Max: 480
Min: 92
>>>
Upvotes: 3
Reputation: 8814
It's a list of lists (of length 1) of tuples, so we have to do just a bit of legwork.
import pandas as pd
df = pd.DataFrame([d[0] for d in data])
df[0].max()
df[1].max()
df[0].mean()
df[0].min()
Tons of operations are supported.
Upvotes: 3
Reputation: 2208
max([x[0][0] for x in list])
min([x[0][0] for x in list])
This takes max and min for the first column. similarly for the second column you can do this :
max([x[0][1] for x in list])
min([x[0][1] for x in list])
Upvotes: -1