6

I'm trying to figure out how I can take a list of integers and return all the items in that list into another list as their max value, but with their index.

So I need to be able to do this without using enumeration, lambda, numpy, or anything of that sort. It has to be really basic methods for lists. Basically, things like append, max, etc. . . If for statements are fine too.

To clarify what I'm trying to do, say I have a list: [4, 34, 0, 0, 6, 34, 1] I want it to return [1, 5]

Stewie Griffin
  • 14,889
  • 11
  • 39
  • 70
user3324536
  • 325
  • 1
  • 6
  • 13
  • 3
    Which was also your question; if there is a problem with the answers there, please comment rather than opening a new question. Make your rules clearer: `max` is OK but not `enumerate`? Both are [built in functions](http://docs.python.org/library/functions.html). Is `range` acceptable? Are list comprehensions? – jonrsharpe Feb 19 '14 at 22:39

3 Answers3

9

Simplest approach:

in [24]: a = [4, 34, 0, 0, 6, 34, 1]

In [25]: j=0

In [26]: M=[]

In [27]: m = max(a)

In [28]: for i in a:
    if i==m:
        M.append(j)
    j+=1
   ....:     

In [29]: M
Out[29]: [1, 5]

Using list-comprehension and enumerate, the above can be shortened to:

In [30]: [i for i, x in enumerate(a) if x == max(a)]
Out[30]: [1, 5]
Fredrik Pihl
  • 44,604
  • 7
  • 83
  • 130
  • 4
    Warning: With the list comprehension max is run for every iteration of the loop which is inefficient. However, it'd be fine to calculate it outside like: `max_a = max(a); max_inds = [i for i, x in enumerate(a) if x == max_a]`. – Matthew D. Scholefield Jan 31 '18 at 18:17
1

A "fully manual" approach, using none of those pesky standard library functions:

def get_max_indices(vals):
    maxval = None
    index = 0
    indices = []
    while True:
        try:
            val = vals[index]
        except IndexError:
            return indices
        else:
            if maxval is None or val > maxval:
                indices = [index]
                maxval = val
            elif val == maxval:
                indices.append(index)
            index = index + 1

What it loses in brevity, it gains in... not much.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
0

There are better ways, but if you really want to make it hard..

max_val = max(funny_list)
i = 0
ind = []
for val in funny_list:
    if val == max_val:
        ind.append(i)
    i = i + 1
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
rokuingh
  • 257
  • 2
  • 10
  • 5
    Using `max` and `list` as names is a really bad idea – jonrsharpe Feb 19 '14 at 22:57
  • Hey, so I've tried this option myself. I have a tester that runs through cases of my function. funny_list for me is a bunch of cases with a heavily nested list. The thing is, When I run this it only ever returns "none". Is there something I have to add at the end to return the list and values inside it? – user3324536 Feb 21 '14 at 14:26
  • The answer is in 'ind', so try returning or printing ind to see the indices of the max values. – rokuingh Feb 21 '14 at 16:37
  • I put return ind at the very last line right under i = i + 1 Bu that just gives me either [] or [0], i'm not getting all the rest of the values. – user3324536 Feb 21 '14 at 17:53
  • I think you should check your inputs. If you use the original list that you posted with this code you will get the answer you requested. If you still can't figure it out, break it down as much as you can and then post another question. Good luck! – rokuingh Feb 21 '14 at 22:04