PYTHON - finding the maximum every 10 integers in an array

I have a large array of integers, and I need to print a maximum of 10 integers and its corresponding index in the array as a pair.

ex. (max_value, index of max_value in array)

I can successfully find the maximum value and the corresponding index within the first 10 integers, however, I had a problem with the loop over the entire array.

I tried using:

a = some array of integers

split = [a[i:i+10] for i in xrange(0, len(a), 10)] 

for i in split:
    j = max(i) 
    k = i.index(max(i))
    print (j,k)

The problem with this method is that it breaks my array into pieces of 10, so max_values ​​are correct, but the indices are inaccurate (all indexes are between 0-10.) I need to find a way to do this that doesn't break my array into pieces so that the original indexes are preserved. I'm sure there is an easier way to scroll to find the maximum values, but I can't figure it out.

+4
8

, example , split 2d, :

[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]]

, for , . , .. , for , 10. , :

a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
split = [a[i:i+10] for i in xrange(0, len(a), 10)] 
counter = 0

for i in split:
    j = max(i) 
    k = i.index(max(i))
    print (j,k+counter)
    counter += 10

+3

, . :

a=list(range(5,35))
split = [a[i:i+10] for i in xrange(0, len(a), 10)] 

for ind,i in enumerate(split):
    j = max(i) 
    k = i.index(j)
    print (j,k+ind*10)

(14, 9)
(24, 19)
(34, 29)
+5

:

a = some array of integers

split = [a[i:i+10] for i in xrange(0, len(a), 10)] 

for index, i in enumerate(split):
    j = max(i) 
    k = i.index(max(i))
    print (j, k+10*index)
+5

, , split, , .

a = some array of integers

split = [a[i:i+10] for i in xrange(0, len(a), 10)] 

for i in range(len(split)):
    #Now instead of being the list, i is the index, so we can use 10*i as a counter
    j = max(split[i]) 
    #j = max(i) 
    k = split[i].index(j) + 10*i #replaced max(i) with j since we already calculated it.
    #k = i.index(max(i))
    print (j,k)

, split, split python. , split_list separated , split().

+1

toolz partition_all, , - .

import toolz
ns = list(range(25))
[max(sublist) for sublist in toolz.partition_all(10, ns)]

[9, 19, 24].

+1

numpy :

import numpy as np

a = np.random.randint(1,21,40)  #40 random numbers from 1 to 20

b = a.reshape([4,10])  #shape into chunks 10 numbers long

i = b.argsort()[:,-1]  #take the index of the largest number (last number from argsort) 
                       #  from each chunk. (these don't take into account the reshape)

i += np.arange(0,40,10)  #add back in index offsets due to reshape

out = zip(i, a[i])  #zip together indices and values
+1

, zip, :

n=10
for grp in zip(*[iter(enumerate(some_list))]*n):
    grp_max_ind, grp_mv=max(grp, key=lambda t: t[1])
    k=[t[1] for t in grp].index(grp_mv)
    print grp_mv, (grp_max_ind, k)

izip Python 2, ( Python 3)

from itertools import izip 
for grp in izip(*[iter(enumerate(some_list))]*n):
    grp_max_ind, grp_mv=max(grp, key=lambda t: t[1])
    k=[t[1] for t in grp].index(grp_mv)
    print grp_mv, (grp_max_ind, k)

Zip , n

+1

numpy. , .. 1 V ( ) L:

import numpy as np
V = 1000
L = 45 # method works with arrays not multiples of 10
a = np.random.randint(1, V, size=L)

- N:

import numpy as np
N = 10 # example "split" size
sa = np.array_split(a, range(N, len(a), N))
sind = [np.argpartition(i, -1)[-1] for i in sa]
ind = [np.ravel_multi_index(i, (len(sa), N)) for i in enumerate(sind)]
vals = np.asarray(a)[np.asarray(ind)]
split_imax = zip(vals, ind) # <-- output
+1

Source: https://habr.com/ru/post/1682801/


All Articles