Python新手,並且一直在學習數組。我堅持一個足夠簡單的問題,需要一個解決方案。我有兩個陣列:Python:比較兩個數組的所有元素並修改第二個數組
a = [2.0, 5.1, 6.2, 7.9, 23.0] # always increasing
b = [5.1, 5.5, 5.7, 6.2, 00.0] # also always increasing
和欲得到的數組是:
c = [0.0, 5.1, 6.2, 0.0, 0.0] # 5.5, 5.7, 00.0 from 'b' were dropped and rearranged such that position of equivalent elements as in 'a' are maintained
我比較兩者 '一' & 'B' 使用numpy的如:
y = np.isclose(a, b)
print y
# [False False False False False]
(或者)我也嘗試過這樣的事情,這是不正確的方式(我認爲):
c = np.zeros(len(a))
for i in range (len(a)):
for j in range (len(a)):
err = abs(a[i]-b[j])
if err == 0.0 or err < abs(1):
print (err, a[i], b[j], i, j)
else:
print (err, a[i], b[j], i, j)
如何從這裏開始獲取'c'?
嘗試'y = np.isclose(a,b,atol = 0.05)'。 – Norman
它不影響結果。把'atol = 0.5'給出'[False True False False]',這是'c'bool明智的。 – rNov
我是否簡單地將'a'的元素值複製到'True'值的位置?還是有更好的方法呢? – rNov