大家好,我將如何繼續轉換 numpy 陣列:
[[ 8.82847075 -5.70925653]
[ 1.07032615 -1.77975378]
[-10.41163742 -0.33042086]
[ 0.23799394 5.5978591 ]
[ 7.7386861 -4.16523845]]
對于我在 Python 3.10 中想要的東西。這包括將鍵和值四舍五入到最接近的整數:
{'9':-6, '1':-2, '-10':0, '0':6, '8':-4}
uj5u.com熱心網友回復:
dict(zip(*d.round().astype(int).T))
Out: {9: -6, 1: -2, -10: 0, 0: 6, 8: -4}
資料
d = np.array([[ 8.82847075, -5.70925653],
[ 1.07032615, -1.77975378],
[-10.41163742, -0.33042086],
[ 0.23799394, 5.5978591 ],
[ 7.7386861 , -4.16523845]])
uj5u.com熱心網友回復:
以下應該作業
a = np.round(a)
d = dict(zip(a[:, 0].astype(str), a[:, 1]))
注意:相等的鍵將合并。
uj5u.com熱心網友回復:
我會用串列理解來做到這一點:
import numpy as np
data = np.array([[8.82847075,-5.70925653],[1.07032615,-1.77975378],[-10.41163742,-0.33042086],[0.23799394,5.5978591],[7.7386861,-4.16523845]])
data_dict = { int(round(x)):int(round(y)) for x,y in data}
data_dict
>>>{9: -6, 1: -2, -10: 0, 0: 6, 8: -4}
uj5u.com熱心網友回復:
嘗試使用此功能:
arrayToDictionary(x:list):
tempDictionary = dict()
for value in x:
x = str(int(x[0]))
y = int(x[1])
tempDictionary[x] = y
return tempDictionary
# then call the function using:
print(arrayToDictionary(yourarray))
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/344669.html
