遍历一个numpy的阵列之类的函数枚举 [英] Iterating over a numpy array with enumerate like function
本文介绍了遍历一个numpy的阵列之类的函数枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要numpy的阵列到我的一些code的。我试图遍历数组。
I want to numpy arrays into some of my code. I am trying to iterate over an array.
import numpy as np
a=10.0
b=55.0
y=np.asarray([11,30,54,7,22,5,15,65,15,6])
I =[y[i] / (a + (i+1) * b) for i in range(0, len(y))]
print I
>>>
[0.16923076923076924, 0.25, 0.30857142857142855, 0.030434782608695653, 0.077192982456140355, 0.014705882352941176, 0.037974683544303799, 0.14444444444444443, 0.029702970297029702, 0.010714285714285714]
到目前为止,我切换了迭代方法枚举这将给予同样的结果,但用更快的速度
So far I switched the iteration method to enumerate which will give the same results but with faster speed
J=[y[i] / (a + (i+1) * b) for i, item in enumerate(y)]
print J
在Python的大熊猫有创造[1,2,3,4,5,6,... n]和然后使用该数组中,而不是使用一个for循环阵列的功能。这是可以在numpy的和它使这一进程更快?
In Python pandas there is a function to create an array of [1,2,3,4,5,6,...n] and then using the values in the array instead of using a for loop. Is this available in numpy and does it make the process faster?
推荐答案
您可以不用带的 np.arange :
>>> c = a + b*(np.arange(1, len(y)+1))
>>> y/c
array([ 0.16923077, 0.25 , 0.30857143, 0.03043478, 0.07719298,
0.01470588, 0.03797468, 0.14444444, 0.02970297, 0.01071429])
这篇关于遍历一个numpy的阵列之类的函数枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文