I'm working on a script that converts ids of school names to actual school names structured in a numpy array.
For example
[[1,2,3],[3,6,7]]
becomes
[[school-a,school-b,school-c],[school-c,school-f,school-g]
The school and ids sit together in a python dictionary.
I've tried doing this:
for x in np.nditer(finalarray, op_flags=['readwrite']):
x[...] = school_ids.get(int(x))
print(school_ids.get(int(x)))
print(finalarray)
but that gave the error:
ValueError: invalid literal for int() with base 10: 'school-a'
it's important that the structure of the numpy array stays the same, because I also thought of just iterating every item, but then the structure is lost.
Using the solution from this post :
x = np.array([[1,1,3], [2,2,2]])
d = {1: 'a', 2:'b', 3:'c'}
np.vectorize(d.get)(x)
>> array([['a', 'a', 'c'],
['b', 'b', 'b']], dtype=object)
suppose I have a dict :
dictt = {
0: 'school-a',
1: 'school-b',
2: 'school-c',
3: 'school-d',
4: 'school-e',
5: 'school-f',
6: 'school-g',
7: 'school-h',
8: 'school-i'
}
_ids = np.array([[1,2,3],[3,6,7]])
school_ids = np.array(list(dictt.values()))
print school_ids[_ids-1]
got:
[['school-a' 'school-b' 'school-c']
['school-c' 'school-f' 'school-g']]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.