2013-05-26 108 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)]