Need max values in list of tuples, based on position

Pancho Pancho.Jones at proton.me
Sun Nov 13 07:37:10 EST 2022


On 11/11/2022 19:56, DFS wrote:

> Edit: found a solution online:
> -----------------------------------------------------------------
> x = [(11,1,1),(1,41,2),(9,3,12)]
> maxvals = [0]*len(x[0])
> for e in x:
>      maxvals = [max(w,int(c)) for w,c in zip(maxvals,e)]
> print(maxvals)
> [11,41,12]
> -----------------------------------------------------------------
> 
> So now the challenge is making it a one-liner!
> 

  x = [(11,1,1),(1,41,2),(9,3,12)]
  print(functools.reduce( lambda a,b : [max(w,c) for w,c in zip(a,b)],
		x, [0]*len(x[0])))




More information about the Python-list mailing list