我正在嘗試比較嵌套 for 回圈中二維陣列的值,在這種情況下我似乎無法獲得正確的關系操作。
所以我正在回圈遍歷二維陣列并將其值與另一個一維陣列中的值進行比較,我度過了非常有趣的一天。
import numpy as np
packetsDb = np.empty((0,4))
head = [['AAA', 'BBB', 'CCC', 'DDD']]
packet1 = [[255, 256, 257, 258]]
packet2 = [[255, 256, 257, 259]]
test = [255, 256, 257, 259]
packetsDb = np.append(packetsDb, np.array(head), axis=0)
packetsDb = np.append(packetsDb, np.array(packet1), axis=0)
packetsDb = np.append(packetsDb, np.array(packet2), axis=0)
for x in packetsDb:
for y in x:
print(test[0], y, test[0] == y)
//Result
255 AAA False
255 BBB False
255 CCC False
255 DDD False
255 255 False //Whats happening here
255 256 False
255 257 False
255 258 False
255 255 False //and here
255 256 False
255 257 False
255 259 False
uj5u.com熱心網友回復:
結果一開始確實是違反直覺的,但是一旦你列印了型別,它就會變得更加清晰:
for x in packetsDb:
for y in x:
print(test[0], y, test[0] == y, type(test[0]), type(y))
//Result
255 AAA False <class 'int'> <class 'numpy.str_'>
...
test[0] 和 y 在列印時看起來相同,但實際上它們是不同的型別,因此比較結果為 False。
當您確實將字串串列附加到 numpy-array 時,numpy 確實將整個陣列轉換為 str 型別,包括能夠表示新資料的整數。將此鏈接視為參考Python - Numpy 陣列中的字串和整數
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/497795.html
