Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Max Value within a List of Lists of Tuple

Tags:

python

list

max

I have a problem to get the highest Value in a dynamic List of Lists of Tuples.
The List can looks like this:

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

Now I loop through the List to get the highest 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 f.e. total = {'name1':1,'name10':20, ..}

I tried a lot to get the maximum Value but I found no conclusion to my problem. I know i must loop through each Tuple in the List and compare it with the next one but it dont know how to code it correct.

Also I can use the function max() but it doesnt work with strings and integers. f.e. a = [ ('a',5),('z',1)] -> result is max(a) ---> ('z',1) obv 5 > 1 but z > a so I tried to expand the max function with max(a, key=int) but I get an Type Error.

Hope you can understand what I want ;-)

UPDATE

Thanks so far.

If I use itertools.chain(*adymlist) and max(flatlist, key=lambda x: x[1])
I will get an exception like : max_word = max(flatlist, key=lambda x: x[1]) TypeError: 'int' object is unsubscriptable

BUT If I use itertools.chain(adymlist) it works fine. But I dont know how to summate all integers from each Tuple of the List. I need your help to figure it out.

Otherwise I wrote a workaround for itertools.chain(*adymlist) to get the sum of all integers and the highest integer in that 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]

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

like image 248
Sv3n Avatar asked Feb 10 '11 23:02

Sv3n


People also ask

How do you find the max in a list of tuples?

To find the min and max values in a list of tuples: Use the min() and max() functions. Pass the key argument to the functions. Select the element in the tuple to be compared.

How do you find the max of a list in a list?

The max() Function — Find the Largest Element of a List. In Python, there is a built-in function max() you can use to find the largest number in a list. To use it, call the max() on a list of numbers. It then returns the greatest number in that list.

Can Max be used with tuple?

max(): gives the largest element in the tuple as an output. Hence, the name is max(). max(): gives the sum of the elements present in the tuple as an output.

Does Max work on lists Python?

Python has an in-built max function that you can use to locate the maximum value in an iterable. For example, if we call the max function in a list of strings, it returns the last item with the strings arranged in alphabetical order.


2 Answers

To clarify, looks like you've got a list of lists of tuples. It doesn't look like we care about what list they are in, so we can simplify this to two steps

  • Flatten the list of lists to a list of tuples
  • Find the max value

The first part can be accomplished via itertools.chain (see e.g., Flattening a shallow list in Python)

The second can be solved through max, you have the right idea, but you should be passing in a function rather than the type you want. This function needs to return the value you've keyed on, in this case ,the second part of the tuple

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

Correction

I re-read your question - are you looking for the max value in each sub-list? If this is the case, then only the second part is applicable. Simply iterate over your list for each list

A bit more pythonic than what you currently have would like

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)
like image 151
dfb Avatar answered Nov 03 '22 07:11

dfb


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

You can iterate over the outer list, then over the list of tuples tuples like this:

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 bit more verbose but might be easier to understand.

like image 25
tomger Avatar answered Nov 03 '22 08:11

tomger