2013-05-26 87 views
0

如果你有下面的代码:通行证元素索引

import numpy as np 

def myFunction(element, index): 
    print element, index 

myVector = np.vectorize(myFunction) 
myVector(myArray, currentElementIndex) 
  • 你怎么能传递numpy的矢量化currentElementIndexmyFunction()

在此先感谢!

编辑:我不太确定我应该在哪里得到应用myFunction()的当前项目的索引。我知道如何传递一个数组元素,但不是索引。

编辑:更新与实际的代码:

import numpy as npy 

def getHashValue(character, index): 
    return (ord(character) - ord('a')) ** (index + 1) 

def getNameHash(name): 
    hashValue = getHashValue 
    hashValue = npy.vectorize(hashValue) 
    hashValue(shortName) 
    return 
+1

你真的想做什么?问题在哪里? (我真的不明白你的问题) – jorgeca

+0

嗯,我不确定我应该在哪里得到'myFunction()'应用到的当前项目的索引。我知道如何传递一个数组元素,但不是索引。 –

+0

你能告诉我们你在做什么(一个最小的工作示例),你想要发生什么?你的代码如何不给你'myFunction'正在被应用的项目?这正是它要打印的内容。 – jorgeca

回答

1

np.vectorize是在numpy的一个方便的函数,它的是标量的值(“数字”)的工作原理的函数,并输出上numpy的工作的功能数组(具有所有的优点,例如广播)。

就你而言,你并不真的需要这些,所以使用enumerate的列表理解正是你正在寻找的。我猜你的代码是为了做到这一点:

def getHashValue(character, index): 
    return (ord(character) - ord('a')) ** (index + 1) 

def getNameHash(name): 
    return [getHashValue(c, i) for i, c in enumerate(name)]