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
#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


Messages In This Thread
RE: finding 2 max values in an array in python - by nilamo - Oct-17-2018, 09:17 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Create array of values from 2 variables paulo79 1 1,168 Apr-19-2022, 08:28 PM
Last Post: deanhystad
  Creating a numpy array from specific values of a spreadsheet column JulianZ 0 1,237 Apr-19-2022, 07:36 AM
Last Post: JulianZ
  Calculate next rows based on previous values of array divon 0 1,894 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 7,457 Mar-02-2021, 01:54 AM
Last Post: Jeremy7
  Finding an element in a 1d list in a 2d array lionrocker221 0 1,901 Jun-27-2020, 04:50 PM
Last Post: lionrocker221
  Finding Max and Min Values Associated with Unique Identifiers in Python ubk046 1 2,164 May-08-2020, 12:04 PM
Last Post: anbu23
  Finding nearest point of a Multidigraph in Python 3.7 stixmagiggins 5 3,940 Aug-24-2019, 08:51 AM
Last Post: ThomasL
  Help with finding correct topic in Python learning yahya01 1 2,283 Jun-06-2019, 05:01 PM
Last Post: buran
  change array column values without loop khalidreemy 2 3,941 May-05-2019, 09:05 AM
Last Post: DeaD_EyE
  finding problems connecting python to sqlite Dennis 1 2,379 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