The maximum value in the list of tuple lists

I have a problem getting the highest value in a dynamic list of tuple lists.
The list may look like this:

adymlist = [[('name1',1)],[('name2',2),('name3',1), ...('name10', 20)], ...,[('name m',int),..]] 

Now I am browsing the list to get the maximum value (integer):

 total = {} y=0 while y < len(adymlist): if len(adymlist) == 1: #has the List only 1 Element -> save it in total total[adymlist[y][0][0]] = adymlist[y][0][1] y += 1 else: # here is the problem # iterate through each lists to get the highest Value # and you dont know how long this list can be # safe the highest Value in total fe total = {'name1':1,'name10':20, ..} 

I tried a lot to get the maximum value, but I did not find any conclusion about my problem. I know that I have to scroll through each Tuple in the list and compare it with the next, but he does not know how to encode it correctly.

I can also use the max() function, but it does not work with strings and integers. FE a = [ ('a',5),('z',1)] β†’ the result max(a) ---> ('z',1) obv 5> 1, but z> a, so I tried extend the max function with max(a, key=int) , but I get Type Error.

I hope you understand what I want; -)

<strong>

UPDATE

Thanks, bye.

If I use itertools.chain(*adymlist) and max(flatlist, key=lambda x: x[1])
, I will get an exception, for example: max_word = max (flatlist, key = lambda x: x [1]) TypeError: the object 'int' is not subject to subscription

BUT if I use itertools.chain(adymlist) , it works fine. But I do not know how to sum all the integers from each tuple from the list. I need your help to figure this out.

Otherwise, I wrote a workaround for itertools.chain(*adymlist) to get the sum of all integers and the highest integer in this list.

 chain = itertools.chain(*adymlist) flatlist = list(chain) # flatlist = string, integer, string, integer, ... max_count = max(flatlist[1:len(flatlist):2]) total_count = sum(flatlist[1:len(flatlist):2]) # index of highest integer idx = flatlist.index(next((n for n in flatlist if n == max_count))) max_keyword = flatlist[idx-1] 

He still does what I want, but isn't it dirty?

+7
source share
2 answers

To clarify, it looks like you have a list of tuple lists. It doesn't seem like we care about which list they are on, so we can simplify this to two steps.

  • Smooth list of lists in tuple list
  • Find the maximum value

The first part can be done through itertools.chain (see, for example, Smoothing a Small List in Python )

The second can be solved with max, you have the right idea, but you should pass a function, not the type you want. This function should return the value on which you keyed, in this case the second part of the tuple

 max(flatlist, key=lambda x: x[1]) 

Correction

I am re-reading your question - are you looking for the maximum value in each sub-list? If so, then only the second part is applicable. Just iterate over the list for each list.

A little more pythonic than what you like right now

 output = [] for lst in lists: output.append( max(flatlist, key=lambda x: x[1]) ) 

or

 map(lambda x: max(x, key=lambda y: y[1]) , lists) 
+16
source

As spintheblack says, you have a list of tuple lists. I assume that you are looking for the highest integer value of all tuples.

You can iterate over the external list, and then over the list of tuples of tuples:

 max_so_far = 0 for list in adymlist: for t in list: if t[1] > max_so_far: max_so_far = t[1] print max_so_far 

This is a little more verbose, but it may be easier to understand.

+4
source

All Articles