[英]How do I change id values in a numpy array to a string numpy array with a dictionary
I'm working on a script that converts ids of school names to actual school names structured in a numpy array. 我正在研究将学校名称的ID转换为以numpy数组结构的实际学校名称的脚本。
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. 学校和id一起坐在python字典中。
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. 保持numpy数组的结构不变是很重要的,因为我还想到了仅迭代每个项目,但是随后结构丢失了。
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']]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.