I think the following code fragment can help you in getting the max and sorted max values. Sorry can't help you in putting them in excel though
for entry in large:
max_list=[]
for z in zip(*entry[1]):
max_list.append(max(z))
print max_list
max_list.sort(reverse=True)
print max_list
[6, 5, 8]
[8, 6, 5]
[2, 15, 5]
[15, 5, 2]
Baically for every entry in large dataset, you have to process entry1 list (which is a list of tuples). Using zip you can 'zip' through multiple data-structures simultaneously.
zip(*entry[1])
This line basically unrolls all the tuples in your list and loops through them concurrently. Thus during first iteration, all the first elements of all the tuples are selected and stored as a tuple.
Using max function, you can get the maximum value and store it in a separate list (A new list for each entry). Then you can sort that list in reverse to get what you want
EDIT
Just noticed you need the absolute max. So the code fragment gets modified as
for entry in large:
max_list=[]
for z in zip(*entry[1]):
max_list.append(max(abs(t) for t in z))
print "Absolute max",max_list
max_list.sort(reverse=True)
print "Sorted Absolute Max",max_list
Input
large = [('5501', [(4, -5, 8), (6, -4, -6)]), ('2222', [(2, -4, 5), (1, -15, -4)])]
Output
Absolute max [6, 5, 8]
Sorted Absolute Max [8, 6, 5]
Absolute max [2, 15, 5]
Sorted Absolute Max [15, 5, 2]
EDIT2
Since you asked about zip and editted code, I'll try to explain this.
zip() is an in-built python function which helps you in iterating over multiple iterables (lists,tuples and any other thing you can iterate over) parallely.
>>> a=[1,2,3,]
>>> b=[4,5,6,]
>>> zip(a,b)
[(1, 4), (2, 5), (3, 6)]
The i-th tuple of the result is a collection of i-th elements of the input lists
In your case entry[1]
is the list of tuples you want to 'zip' over, but zip only works if we give it multiple lists not a single list of lists!
This is where * operator comes in handy. It will splat you list of lists into multiple lists.
>>> def printme(*a):
... for i in a:
... print i
...
>>> m=[(1,2,),(3,4,),(5,6,),]
>>> printme(m)
[(1, 2), (3, 4), (5, 6)]
>>> printme(*m)
(1, 2)
(3, 4)
(5, 6)
And if you want to find the maximum value in a list, you can use the in-built max() function.
Now we have all the theoretical elements needed to solve your problem. Lets say
>>> entry=('5501', [(4, -5, 8), (6, -4, -6)])
>>> entry[1]
[(4, -5, 8), (6, -4, -6)]
What you want to do is zip over these tuples so that all the first elements form a separate tuple, all the second elements form a separate tuple and so on.
>>> zip(entry[1])
[((4, -5, 8),), ((6, -4, -6),)]
No Good!!! Lets use the *
>>> zip(*entry[1])
[(4, 6), (-5, -4), (8, -6)]
Perfect! Now for each tuple in the list, all we need to do is do a max() to find out the maximum value and store it somewhere.
>>> for z in zip(*entry[1]):
... print "I have",z
... print "max is",max(z)
...
I have (4, 6)
max is 6
I have (-5, -4)
max is -4
I have (8, -6)
max is 8
So if we initialize an empty list and append the maximum values to it, we get the maximum value list! Hence the first code that I wrote
max_list=[]
for z in zip(*entry[1]):
max_list.append(max(z))
print max_list
Gives us
[6, -4, 8]
To sort the max_list itself, we use sort method of the lists
max_list.sort(reverse=True)
print max_list
Gives us
[8, 6, -4]
But we need the absolute max of the values.
In the above code fragment, z
, holds the values. So we need a way to convert those values into their absolute. The in-built abs(), only works for a number so we need to apply it to each element of z
>>> for z in zip(*entry[1]):
... print z
... new_z = []
... for i in z:
... new_z.append(abs(i))
... print new_z
...
(4, 6)
[4, 6]
(-5, -4)
[5, 4]
(8, -6)
[8, 6]
Or we can be pythonic and shorten it to
>>> for z in zip(*entry[1]):
... print z
... new_z=[abs(i) for i in z]
... print new_z
...
(4, 6)
[4, 6]
(-5, -4)
[5, 4]
(8, -6)
[8, 6]
But we also need the maximum value of new_z
. That is easy max(new_z)
. Our code now is
max_list=[]
for z in zip(*entry[1]):
new_z=[abs(i) for i in z]
max_list.append(max(new_z))
print max_list
Which gives us
[6, 5, 8]
This is what we want. But wait, we can still shorten the code
new_z=[abs(i) for i in z]
max_list.append(max(new_z))
Can be converted to a single line
max_list.append(max( abs(t) for t in z ))
Which brings us to the final code
for entry in large:
max_list=[]
for z in zip(*entry[1]):
max_list.append(max(abs(t) for t in z))
print "Absolute max",max_list
max_list.sort(reverse=True)
print "Sorted Absolute Max",max_list
Hope this helps