weston6142
weston6142

Reputation: 191

Maximum value and Minimum value from list of lists python 2.7

I have a list of lists with pairs inside the inner lists as shown below.

odds_list = [[(105, -116), (105, -115), (-115, -105), (-115, -105), (100, -120), (-115, 105), (100, -120), (105, -125), (-115, -105), (-115, -105)], [(-108, -102), (110, -130), (-110, -110), (-108, -112), (-115, -105), (-105, -105), (-115, -105), (-110, -110), (-110, -110), (-110, -110)]

There are 10 pairs in each inner list and there are 13 inner lists (I did not copy and paste them all over in the example).

I need the maximum and minimum value of the left pair value (out of all the pairs in that specific inner list), and the maximum and minimum value of the right pair value (out of all the pairs in that specific inner list) for each of the inner lists.

How would I go about this using python?

For the first list of 10 pairs, it would be

Max_Value_Left_List_1 = 105
Min_Value_Left_List_1 = -115
Max_Value_Right_List_1 = 105
Min_Value_Right_List_1 = -125

Note: The amount of inner lists in this case is 10 but this is subject to change but there will always be 10 pairs in an inner list.

Upvotes: 0

Views: 98

Answers (2)

quan
quan

Reputation: 1

you can use pandas DataFrame help you calculate it quickly:

>>> data=[]
>>> for list_e in odds_list:
...     for left,right in list_e:
...         data.append([left,right])
...
>>> df=DataFrame(data)
>>> df.describe()
            0          1
count   40.000000   40.00000
mean   -46.650000 -100.82500
std    100.986557   48.45981
min   -115.000000 -130.00000
25%   -115.000000 -116.75000
50%   -110.000000 -110.00000
75%    100.000000 -105.00000
max    125.000000  105.00000
>>> df[0].min()
-115
>>> df[0].max()
125
>>> df[1].max()
105
>>> df[1].min()
-130

Upvotes: 0

Zachary Cross
Zachary Cross

Reputation: 2318

Here's a way to calculate the result for each inner list and then store each inner-list's result as a dictionary inside a list - one dictionary per inner-list.

max_and_min = []  # This will contain a dictionary per inner list

for inner_list in odds_list:
    lefts = [x[0] for x in inner_list]
    rights = [x[1] for x in inner_list]

    result_dict = {'max_left': max(lefts), 'min_left': min(lefts), 'max_right': max(rights), 'min_right': min(rights)}
    max_and_min.append(result_dict)

# Get the result dict for the first inner list
print(max_and_min[0])

Upvotes: 2

Related Questions