Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 finding 2 max values in an array in python Akankshha Unladen Swallow Posts: 1 Threads: 1 Joined: Oct 2018 Reputation: 0 Likes received: 0 #1 Oct-15-2018, 05:51 PM 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? buran Posts: 6,102 Threads: 103 Joined: Sep 2016 Reputation: 468 Likes received: 1277 #2 Oct-15-2018, 05:55 PM What have you tried? Show us your code in python tags. micseydel and nilamo like this post woooee Minister of Silly Walks Posts: 474 Threads: 0 Joined: Feb 2018 Reputation: 35 Likes received: 36 #3 Oct-16-2018, 04:38 AM (This post was last modified: Oct-16-2018, 04:38 AM by woooee. Edited 1 time in total.) 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.). marienbad Silly Frenchman Posts: 47 Threads: 10 Joined: Oct 2018 Reputation: 1 Likes received: 6 #4 Oct-16-2018, 11:02 AM ```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)``` LeSchakal Programmer named Tim Posts: 9 Threads: 0 Joined: Oct 2018 Reputation: 0 Likes received: 0 #5 Oct-17-2018, 07:54 PM 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] ``` nilamo Last Thursdayist Posts: 3,257 Threads: 87 Joined: Sep 2016 Reputation: 133 Likes received: 731 #6 Oct-17-2018, 08:03 PM ```>>> 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. buran likes this post volcano63 Verb Conjugator Posts: 564 Threads: 6 Joined: Apr 2017 Reputation: 25 Likes received: 99 #7 Oct-17-2018, 08:16 PM (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. LeSchakal Programmer named Tim Posts: 9 Threads: 0 Joined: Oct 2018 Reputation: 0 Likes received: 0 #8 Oct-17-2018, 08:56 PM 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 nilamo Last Thursdayist Posts: 3,257 Threads: 87 Joined: Sep 2016 Reputation: 133 Likes received: 731 #9 Oct-17-2018, 09:17 PM (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`````` buran likes this post LeSchakal Programmer named Tim Posts: 9 Threads: 0 Joined: Oct 2018 Reputation: 0 Likes received: 0 #10 Oct-17-2018, 09:43 PM (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. « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post Finding Max and Min Values Associated with Unique Identifiers in Python ubk046 1 108 May-08-2020, 12:04 PM Last Post: anbu23 change array column values without loop khalidreemy 2 438 May-05-2019, 09:05 AM Last Post: DeaD_EyE Spyder hides middle array values in variable explorer window davidqvist 0 713 Aug-03-2018, 11:04 AM Last Post: davidqvist Need help for finding cumulative values in a loop Lightning1800 10 1,849 May-23-2018, 07:48 PM Last Post: Lightning1800 Finding all maximum values in a matrix Lightning1800 3 1,165 May-14-2018, 03:55 PM Last Post: Lightning1800 Finding values to draw a line on a curve kesenthilkumar 2 1,345 Sep-19-2017, 09:50 AM Last Post: kesenthilkumar Trouble finding Python code Larz60+ 5 1,768 Jun-02-2017, 01:30 PM Last Post: sparkz_alot Extract values from array mehtamonita 8 4,276 Apr-18-2017, 02:45 PM Last Post: mehtamonita

Forum Jump:

Users browsing this thread: 1 Guest(s)