我試圖了解以下 python 代碼中發生的情況:import numpy as npnumberList1 = [1,2,3]numberList2 = [[4,5,6],[7,8,9]]result = np.dot(numberList2, numberList1)# Converting iterator to setresultSet = set(result)print(resultSet)輸出:{32, 50}我可以看到它正在將每個元素乘以 -so內numberList1每個數組中相同位置的元素。numberList2{1*4 + 2*5 + 3*6 = 32},{1*7+2*8+3*9 = 50}但是,如果我將數組更改為:numberList1 = [1,1,1]numberList2 = [[2,2,2],[3,3,3]]然后我看到的輸出是{9, 6}這是錯誤的方法...并且,如果我將其更改為:numberList1 = [1,1,1]numberList2 = [[2,2,2],[2,2,2]]然后我看到的輸出就是{6}從文檔中:如果 a 是 ND 數組且 b 是一維數組,則它是 a 和 b 的最后一個軸上的和積。我還不夠數學家,無法完全理解這告訴我什么;或者為什么有時輸出的順序會互換。
1 回答

慕村9548890
TA貢獻1884條經驗 獲得超4個贊
aset是一種無序的數據類型 - 它會刪除您的重復項。np.dot不返回迭代器(如您的代碼中所述),但np.ndarray將按照您期望的順序返回:
import numpy as np
numberList1 = [1, 2, 3]
numberList2 = [[4, 5, 6], [7, 8, 9]]
result = np.dot(numberList2, numberList1)
# [32 50]
# <class 'numpy.ndarray'>
# numberList1 = [1, 1, 1]
# numberList2 = [[2, 2, 2], [3, 3, 3]]
# -> [6 9]
添加回答
舉報
0/150
提交
取消