Macsim
Macsim

Reputation: 61

Picking up an item from a list of lists

I'm pretty new in python and have am difficulty doing the next assignment. I am receiving 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 sum up the lists) I need to replace it by the max number in the same place in the other lists. So my result should be None = max(4,2) and receive :

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

If I go through a for loop I don't understand how can I go to the other sub lists and check them out (especially when I don't know how many subs lists I have)

Upvotes: 5

Views: 830

Answers (7)

simpel01
simpel01

Reputation: 1782

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)]

Upvotes: 1

OneCricketeer
OneCricketeer

Reputation: 191681

This one is a bit cleaner to read than the others IMO

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]]

Upvotes: 1

Mikhail Gerasimov
Mikhail Gerasimov

Reputation: 39516

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]]

Upvotes: 1

Bhargav Rao
Bhargav Rao

Reputation: 52071

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 the list comprehension checks if each element is None or not. If not it will print the number, else it will fnd the maximum and print that element.

Another way using 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]]

Upvotes: 2

Damian Chrzanowski
Damian Chrzanowski

Reputation: 479

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

Upvotes: 1

Y2H
Y2H

Reputation: 2537

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 there is a None in the last list you didn’t specify what the code should do. I just added a try and except. You can replace pass with what you want the code to do.

Upvotes: 1

Josh Weinstein
Josh Weinstein

Reputation: 2968

Here is a hint: In Python, a for in loop iterates through all the elements in some iterable. If you have a list of lists, that means each element in the list can also have a for loop applied to it, as in a for loop inside a for loop. You could use this if and only if the maximum depth of a 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 the code for replacing the none value yourself for practice.

Upvotes: 1

Related Questions