I have a variable number of lists and I wanted to find the maximum of each of these numbers from all the lists.
I’m trying to use the map function to map the maximum of all the lists and I need to use the generator expression to pass the parameters in the map function.
I’m using something like this but it does not work:
map(max,x[c] for c in x.keys())
As an example my lists are in a dictionary:
{1: [0, 1, 0, 0, 3, 0],
2: [1, 0, 0, 0, 0, 4, 5],
3: [0, 5, 6, 0, 1, 1]}
You probably noticed that the lists are not all of equal sizes and that’s how I want them to be because of the way I have the data that I need to parse.
The result of this should be: [1, 5, 6, 0, 3, 4, 5]
Please do suggest if there are other ways to doing this.
Above is an example of the list. I cannot use x.values() to retrieve the lists because of the way my actual dictionary is set up. Lists are embedded at the bottom of hierarchy and I need to iterate over the top of hierarchy to retrieve the lists to compare. I must use a generator expression to retrieve all the lists so please provide solutions without the use of x.values()
To get the
maxs past the end of the shortest value, you needizip_longest:It’s known as
zip_longeston Python 3.This is still the way to do it even if you don’t have a simple method to access all of the lists. You can get the lists however you want:
Then use: