Matching an item from a list of lists

I am new to python and have difficulty completing the following task. I get a list of lists with numbers and the word none, for example:

[[1,None],[2,4],[1.5,2]] 

My problem is when I go through None (I need to summarize the lists). I need to replace it with the maximum number in the same place on other lists. Therefore, my result should be None = max(4,2) and get:

  [[1,4],[2,4],[1.5,2]] 

If I go through the for loop, I donโ€™t understand how I can go to other subcategories and check them (especially when I donโ€™t know how many subscription lists I have)

+5
source share
7 answers

Use a nested list comprehension with conditional

 >>> l = [[1,None],[2,4],[1.5,2]] >>> def findMax(j): ... return max(i[j] for i in l) ... >>> [[j if j is not None else findMax(k) for k,j in enumerate(i)] for i in l] [[1, 4], [2, 4], [1.5, 2]] 

Here, list comprehension checks if every element is None or not. If not, it will print the number, otherwise it will be the maximum and print this element.

Another way to use map is

 >>> l = [[1,None],[2,4],[1.5,2]] >>> maxVal = max(map(max, l)) >>> [[j if j is not None else maxVal for k,j in enumerate(i)] for i in l] [[1, 4], [2, 4], [1.5, 2]] 
+2
source

Here's a tip: in Python, the for in loop repeats through all the elements in some iterable. If you have a list of lists, this means that each element in the list can also have an application loop for it, as in a for loop inside a for loop. You can use this if and only if the maximum depth of the list is 2:

 def get_deep_max(deeplst): new = [] for elem in deeplst: for num in elem: new.append(num) return max(new) 

Try writing code to replace the none value for practice.

+1
source

code:

 for idx1, sublist in enumerate(list1): for idx2, element in enumerate(sublist): if element is None: try: sublist[idx2] = max(list1[idx1+1]) except IndexError: pass 

The problem is that if None is not in the last list, you did not specify what the code should do. I just added try and except . You can replace pass with what you want the code to do.

+1
source

My suggestion:

 x = [[1,None],[2,4],[1.5,2]] #your list highest_num = None # assume that 0 can be the highest number in a different situation for each in x:`# find the highest number for another in each: if another > highest_num: highest_num = another for each in xrange(len(x)): # find the None and replace it with the highest number for another in xrange(len(x[each])): if x[each][another] is None: x[each][another] = highest_num 
+1
source
 from contextlib import suppress l = [[1,None],[2,4],[1.5,2]] for sub in l: with suppress(ValueError): i = sub.index(None) # find index of None in sublist (or ValueError) sub[i] = max(s[i] for s in l if s[i] is not None) # replace sublist item with max of sublists in same index break print(l) # [[1, 4], [2, 4], [1.5, 2]] 
+1
source

It's a little cleaner to read than other IMOs

 l = [[1,None],[2,4],[1.5,2]] maxVal = max(map(max, l)) # maps the function 'max' over all the sub-lists for subl in l: for idx,elem in enumerate(subl): # use enumerate to get the index and element if elem is None: subl[idx] = maxVal print l # [[1, 4], [2, 4], [1.5, 2]] 
+1
source

Here is my suggestion:

 l = [[1, None], [2, 4], [1.5, 2]] # l = [[1, None], [2, 4], [1.5, 2]] l1 = zip(*l) # l1 = [(1, 2, 1.5), (None, 4, 2)] m = map(max, l1) # m = [2, 4] l2 = [map(lambda y: m[i] if y is None else y, x) for i,x in enumerate(l1)] # l2 = [[1, 2, 1.5], [4, 4, 2]] ret = zip(*l2) # ret = [(1, 4), (2, 4), (1.5, 2)] 
+1
source

Source: https://habr.com/ru/post/1236733/


All Articles