如何使用 numpy 陣列訪問資料dtype=object?
b = numpy.array({"a":[1,2,3]}, dtype=object)
下面提出了一個IndexError.
print(b["a"])
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
uj5u.com熱心網友回復:
由于您將 dict 傳遞給numpy.array()沒有將其放入串列中,因此這是一個零維陣列。要索引到零維陣列,您可以使用b.item()訪問里面的元素。為了完整起見,要訪問"a"字典中鍵中的資料,您可以使用它。
>>> b.item()["a"]
[1, 2, 3]
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/389944.html
