Python Forum
finding 2 max values in an array in python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
finding 2 max values in an array in python
#1
I have an array in Python and i want to find the two largest values in that array and their corresponding position in that array.
Can anyone please share the approach for it?
Reply
#2
What have you tried? Show us your code in python tags.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Also, what column in each row of the array do you want to compare (assuming it is 2 dimensional and not 3 or 4, etc.).
Reply
#4
a = [1,2,3,4,5]

high1 = 0
high2 = 0

for item in a:
	if item > high1:
		high1 = item
		pos = a.index(item)
		
print str(high1)
print str(pos)

for item in a:
	if item > high2 and item < high1:
		high2 = item
		pos = a.index(item)
		
print str(high2)
print str(pos)
Reply
#5
Assuming your array has only unique values:

max_values = sorted(your_array, reverse=True)[:2]
indices = [your_array.index(value) for value in max_values]
Reply
#6
>>> def n_max(seq, n=2):
...   max_items = sorted(enumerate(seq), key=lambda x: x[1])[-1 * n:]
...   return max_items[::-1]
...
>>> items = [4, 5, 3, 2, 63, 0, 1, 5]
>>> n_max(items, 2)
[(4, 63), (7, 5)]
Here you go, pal. Hopefully this isn't homework, because an instructor wouldn't accept this lol.
So what we do here, is use enumerate() to create a list of index-value pairs, which we then sort by value, using the key argument of sorted(). Once the list is sorted by value, we simply grab the n items off the end of the list, since the largest values are at the end, and reverse it before returning it so the largest item is the first in the result set.
Reply
#7
(Oct-17-2018, 08:03 PM)nilamo Wrote:
>>> def n_max(seq, n=2):
...   max_items = sorted(enumerate(seq), key=lambda x: x[1])[-1 * n:]
...   return max_items[::-1]
...
>>> items = [4, 5, 3, 2, 63, 0, 1, 5]
>>> n_max(items, 2)
[(4, 63), (7, 5)]
....
from operator import itemgetter
def n_max(seq, n=2):
    return sorted(enumerate(seq), key=itemgetter(1), reverse=True)[:n]
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#8
I've tried this:

from operator import itemgetter

def n_max(seq, n=2):
    max_items = sorted(enumerate(seq), key=lambda x: x[1])[-1 * n:]
    return max_items[::-1]

def my_max(seq, n=2):
    max_values = sorted(seq, reverse=True)[:n]
    indices = [seq.index(value) for value in max_values]
    return list(zip(indices, max_values))

def n_max_2(seq, n=2):
    return sorted(enumerate(seq), key=itemgetter(1), reverse=True)[:n]


if __name__ == '__main__':
    import timeit
    print(timeit.timeit('n_max(seq)', setup='seq=[x for x in range(10000)]; from __main__ import n_max', number=1000))
    print(timeit.timeit('my_max(seq)', setup='seq=[x for x in range(10000)]; from __main__ import my_max', number=1000))
    print(timeit.timeit('n_max_2(seq)', setup='seq=[x for x in range(10000)]; from __main__ import n_max_2, itemgetter', number=1000))
and the results are:

2.84397915685567
0.6598219036493851
2.251084051447339
Reply
#9
(Oct-17-2018, 08:56 PM)LeSchakal Wrote: and the results are:

2.84397915685567
0.6598219036493851
2.251084051447339

Ok, but because you use .index(), it's slower the more results you want (I believe O(n) is what it'd be called). For example, if you change it to be 1) an unsorted starting list (sorted starting list is cheating kind of), and b) more than just the max(2), you'll see a huge difference. Here's an example (I moved to a global seq so each has a fair starting point):
if __name__ == '__main__':
    import random, timeit
    seq = [x for x in range(10000)]
    random.shuffle(seq)

    print(timeit.timeit('n_max(seq, 100)', setup='from __main__ import seq, n_max', number=1000))
    print(timeit.timeit('my_max(seq, 100)', setup='from __main__ import seq, my_max', number=1000))
    print(timeit.timeit('n_max_2(seq, 100)', setup='from __main__ import seq, n_max_2, itemgetter', number=1000))
Output:
3.9099400770000003 10.747478319 3.6070847269999984
Reply
#10
(Oct-17-2018, 09:17 PM)nilamo Wrote: Ok, but because you use .index(), it's slower the more results you want (I believe O(n) is what it'd be called). For example, if you change it to be 1) an unsorted starting list (sorted starting list is cheating kind of), and b) more than just the max(2), you'll see a huge difference.

Thanks for the explanation.

I was afraid of this a little bit, but never thought it would happen so early.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Create array of values from 2 variables paulo79 1 1,055 Apr-19-2022, 08:28 PM
Last Post: deanhystad
  Creating a numpy array from specific values of a spreadsheet column JulianZ 0 1,074 Apr-19-2022, 07:36 AM
Last Post: JulianZ
  Calculate next rows based on previous values of array divon 0 1,716 Nov-23-2021, 04:44 AM
Last Post: divon
  Indexing [::-1] to Reverse ALL 2D Array Rows, ALL 3D, 4D Array Columns & Rows Python Jeremy7 8 6,957 Mar-02-2021, 01:54 AM
Last Post: Jeremy7
  Finding an element in a 1d list in a 2d array lionrocker221 0 1,787 Jun-27-2020, 04:50 PM
Last Post: lionrocker221
  Finding Max and Min Values Associated with Unique Identifiers in Python ubk046 1 2,006 May-08-2020, 12:04 PM
Last Post: anbu23
  Finding nearest point of a Multidigraph in Python 3.7 stixmagiggins 5 3,659 Aug-24-2019, 08:51 AM
Last Post: ThomasL
  Help with finding correct topic in Python learning yahya01 1 2,167 Jun-06-2019, 05:01 PM
Last Post: buran
  change array column values without loop khalidreemy 2 3,689 May-05-2019, 09:05 AM
Last Post: DeaD_EyE
  finding problems connecting python to sqlite Dennis 1 2,259 Dec-10-2018, 02:58 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020