2

If you have the following code :

import numpy as np

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

myVector = np.vectorize(myFunction)
myVector(myArray, currentElementIndex)
  • How can you pass the currentElementIndex value to myFunction() in Numpy vectorization ?

Thanks in advance !

EDIT : I'm not really sure of where I should get the index of the current item to which myFunction() is applied to. I know how to pass an array element, but not the index.

EDIT : Updated with the actual code :

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
4
  • 1
    What do you really want to do? Where's the problem? (I don't really understand your question) Commented May 26, 2013 at 15:03
  • Well I'm not really sure of where I should get the index of the current item to which 'myFunction()' is applied to. I know how to pass an array element, but not the index. Commented May 26, 2013 at 15:04
  • Can you please show us what you're doing (a minimal working example) and what you'd want to happen? How does your code not give you the item myFunction is being applied to? That's exactly what it'll print. Commented May 26, 2013 at 15:14
  • I updated my question with my code. I want to apply ´getHashValue()´ to every characters in the "shortName" string. The issue here is that this function also need the index / position of each character in the string. Commented May 26, 2013 at 15:19

1 Answer 1

1

np.vectorize is a convenience function in numpy that takes a function that works on scalar values ("numbers") and outputs a function that works on numpy arrays (with all its perks, such as broadcasting).

In your case, you don't really need any of that, so a list comprehension using enumerate is exactly what you're looking for. I guess your code meant to do this:

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

def getNameHash(name):
    return [getHashValue(c, i) for i, c in enumerate(name)]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.