Need max values in list of tuples, based on position

DFS nospam at dfs.com
Sun Nov 13 11:24:31 EST 2022


On 11/13/2022 7:37 AM, Pancho wrote:
> 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])))

noice!



More information about the Python-list mailing list