2017-08-01 92 views
4

我有一个大的整数数组,和我需要打印的最大每10个整数的和与其对应的索引阵列为一对英寸PYTHON - 查找最大每10个整数的数组中的

ex. (max_value, index of max_value in array) 

我可以在前10个整数中成功找到最大值和相应的索引,但是我无法遍历整个数组。

我已经尝试使用:

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) 

这种方法的问题是,它分裂我的阵列成10块这样的max_values是正确的,但指标是不准确的(所有的指标都在0 -10)。 我需要找到这样做的是使原来的索引保留不我阵列分割成块的方式。我敢肯定有循环通过寻找最大价值的一个更简单的方式,但我似乎无法弄清楚。

+1

修复你的间距请,空格在Python –

+0

是必不可少的做你的数组中重复的任何值? – depperm

+1

如果你添加了'enumerate',那么可以计算索引,然后执行'q * 10 + i',其中q是枚举计数器 – depperm

回答

3

所以用一个例子阵列的调试,我们发现split返回这样一个二维表:

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

而且每次for循环运行,它通过这些列表中的一个才能。首先,它穿过第一内部列表,然后第二个等等。所以每次的for循环跳转到下一个列表的时候,我们只需添加10由于列表可以在他们超过2列出,我们存储我们需要增加数量在一个变量,并添加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 

您可以test it here

1

您需要遍历,以便通过列表迭代,但是我们可以改变你的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已经在蟒蛇的功能。也许split_listseparated或者看起来不象split()功能的其他名称。

+0

我刚刚添加到我的答案哈哈,因为我正在阅读我认识到 –

+0

@dawg没有名为split的函数..只有字符串的方法,所以没有跺脚。 'whatever.split = something_else'会是一个问题,但不是这个 – Aaron

5

你需要计算当前窗口前出现的元素个数。这将做的工作:

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) 
+1

似乎每个人都想要计算两次'j',并且每次都会稍微变慢一点:P(不管它会有真正的影响力......) – Aaron

+0

微观优化哥们!但是,是的,您应该在生产场景中重新使用'j'。这里关于堆栈溢出这一切都是关于清晰度和熟悉程度。 :) –

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