0

Suppose I have a list [['apple', 'tree', 4, 5], ['orange', 'bush', 6, 3]]. There isn't always 2 nested lists, could be more. I know how I would compare indexes 2 from this specific list, but lets say there's 6 nested lists.

How would I compare index 2 in all of them, and then store the list with the largest value in its second index. The point is I don't know how many lists there are and need to make it work for any amount. There is a precondition and its that the sublists are all the same length and that the second index will contain an integer.

This is a question for school so I just need help with the basic idea and not really the whole chunk of code as I don't want to plagiarize. I have made attempts, but I get index out of range error. Any help would be appreciated

temp = []
for i in range(len(lst)):
    if lst[i][2] > lst[i+1][2]:
        temp = lst[i]
return temp    `
skrubber
  • 1,095
  • 1
  • 9
  • 18
  • You go up to `len(lst) - 1` but then access `lst[i+1]` which is out of range (your array only goes up to len - 1). If you want to be able to access the list element ahead of where you are currently you should modify your loop range to reflect that – Jessie Oct 29 '17 at 03:16
  • A use case: what happens if the max index is in more than one list? How do you want to account for it? – skrubber Oct 29 '17 at 03:24
  • First you should decide what kind of sorting algorithm you're going to use. Python has a built-in `sort()` and `sorted()` functions, but if you're doing an exercise and trying to implement your own that's fine. After you decide which algorithm you're using, implement it, and make sure it works on the 2nd index of every item. If you want only the max - you can iterate them one by one keeping track of the "max-so-far". – Nir Alfasi Oct 29 '17 at 03:26
  • Ive done sorting in high school in java, but this is an intro course for python for first year uni, we cant really use what we technically dont know. They set restrictions on what we can use because for some other questions we can solve them really quickly using certain methods, but they want us to implement what we learned in class – YellowSnow15 Oct 29 '17 at 03:34

3 Answers3

0

You can just use the key parameter in max:

s = [['apple', 'tree', 4, 5], ['orange', 'bush', 6, 3]]
new_s = max(s, key=lambda x:x[2])

Output:

['orange', 'bush', 6, 3]

Regarding your code now, you will want to assign lst[0] to temp to give your algorithm a benchmark to start with:

def the_max(lst):
   temp = lst[0] #use lst[0] as the first benchmark.
   for i in range(len(lst)):
      if lst[i][2] > temp[2]:
         temp = lst[i]
   return temp
Ajax1234
  • 69,937
  • 8
  • 61
  • 102
0

by specifying key to max function , we can achieve this Here key is second element in list. So adding key=lambda l:l[2] to the normal max function is the solution for this

>>> max( lst ,key=lambda l :l[2])
['orange', 'bush', 6, 3]
>>> 

Read this post for more details on how to use and advantages of using key:lambda What is key=lambda

Sandeep Lade
  • 1,865
  • 2
  • 13
  • 23
0

lst = [['apple', 'tree', 4, 5], ['orange', 'bush', 6, 3],['aa', 'bb', 2, 3]]

print max(lst, key=lambda x:x[2])

or

temp = lst[0]
for i in range(len(lst)):
    temp = temp if temp[2] > lst[i][2] else lst[i]
print temp

output: ['orange', 'bush', 6, 3]

QuantumEnergy
  • 396
  • 1
  • 14