gpt4 book ai didi

python - numpy 数组中的字典?

转载 作者:太空狗 更新时间:2023-10-29 21:28:22 27 4
gpt4 key购买 nike

如何访问数组中的字典?

import numpy as np
x = np.array({'x': 2, 'y': 5})

我最初的想法:

x['y']

Index Error: not a valid index

x[0]

Index Error: too many indices for array

最佳答案

你有一个对象数据类型的 0 维数组。制作这个数组可能是一个错误,但如果你仍然想使用它,你可以通过用一个没有索引的元组索引数组来提取字典:

x[()]

或者调用数组的item方法:

x.item()

关于python - numpy 数组中的字典?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37949409/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com