3

I'm looking for the greatest number out of a list to then obtain the element 1 location before If anyone knows how to do this it would be very much appreciated.

my_list = ['room', 10, 'chamber', 23, 'kitchen', 8]

pos = my_list.aMethodToGetTheGreatestValuePosition()

print('The biggest room is ' + my_list[pos-1])
Miloertas
  • 320
  • 1
  • 3
  • 10

4 Answers4

1

This will do it without having to traverse the list multiple times:

my_list = ['room', 10, 'chamber', 23, 'kitchen', 8]

pos = max(enumerate(my_list[1::2]), key=lambda x: x[1])[0]

print('The biggest room is ' + my_list[2*pos])

This uses enumerate to get the indexes of my_list at the same time it is searching for the max.

Or you can be slightly clever with zip:

print('The biggest room is ' +  max(zip(*[iter(my_list)]*2), key=lambda x: x[1])[0])

which relies upon using the same iterator over my_list to feed successive values to zip (borrowed from another excellent answer). This essentially turns your flat list into a list of tuples which would be a nicer way of storing the original data, if you have that option:

>>> list(zip(*[iter(my_list)]*2))
[('room', 10), ('chamber', 23), ('kitchen', 8)]
Turn
  • 6,656
  • 32
  • 41
0

Here's one way. Note this requires structure, i.e. room, then size sequentially. I don't think that's a bad idea, since you should be using a dictionary or list of tuples anyway.

my_list = ['room', 10, 'chamber', 23, 'kitchen', 8]

pos = my_list.index(max(my_list[1::2]))  # 23

print('The biggest room is ' + my_list[pos-1])  # chamber
jpp
  • 159,742
  • 34
  • 281
  • 339
  • I like this one – jamylak Jan 28 '18 at 02:52
  • Only thing I will note is that `str(...)` is useless – jamylak Jan 28 '18 at 02:57
  • @jamylak Agreed & amended – jpp Jan 28 '18 at 02:58
  • That `index` call means you have to traverse the list twice. – Turn Jan 28 '18 at 04:06
  • @Turn There's nothing wrong with that. I realised this as well, but this is still the best solution because it's very unlikely to be the bottleneck and this solution is nice and simple. It is a completely valid solution – jamylak Jan 28 '18 at 05:42
  • @Turn, agreed. I think the main issue from a holistic perspective is the choice of using a `list` in this way. – jpp Jan 28 '18 at 12:16
  • @jp_data_analysis Agreed! – Turn Jan 28 '18 at 18:28
  • @jamylak Yeah, I didn't mean to say the solution was wrong, just pointing it out as I've found that some people seem think to `index` does some sort of magic reverse hash lookup. I apologize for the terseness. – Turn Jan 28 '18 at 20:24
  • 1
    No problem, i just thought it seemed like people were downvoting for this reason alone so decided to address it in general – jamylak Jan 29 '18 at 01:32
0
my_list = ['room', 10, 'chamber', 23, 'kitchen', 8]

my_list[my_list.index(max(filter(lambda x: isinstance(x, int), my_list)))-1]

Say there are 2 equal maximum numbers in the input list, it will take the first one. That will be a limitation to the solution

be_good_do_good
  • 4,311
  • 3
  • 28
  • 42
-1

How about:

my_list = ['room', 10, 'chamber', 23, 'kitchen', 8]

max_index = max(range(len(mylist)),
                key=lambda i: my_list[i] if isinstance(my_list[i], int) else float('-inf'))
max_name = my_list[max_index-1]

I'd recommend changing your data format though, if you can. A dictionary mapping number to name would (or name to number) would be a lot more natural. Even a list of lists might be easier to deal with.

Blckknght
  • 100,903
  • 11
  • 120
  • 169
  • I'm interested in what you're proposing at the moment, can you give me a link to one of the data format structure so I can learn it myself ? Otherwise, thank you for your time – Miloertas Jan 28 '18 at 08:50
  • 1
    Dictionaries are pretty fundamental to Python. Read about them [in the official Python tutorial](https://docs.python.org/3/tutorial/datastructures.html#dictionaries) (I linked to the dictionary section in particular, but the whole page is probably good for you to read). You could use `{'room': 10, 'chamber': 23, 'kitchen': 8}` or something similar. Much easier to deal with than your interleaved list. – Blckknght Jan 28 '18 at 19:11