簡體   English   中英

如何迭代存儲在列表和字典中的數組

[英]How to iterate over arrays stored in a list and dictionary

我想迭代存儲在列表和字典中的 numpy 數組。 我的列表被命名為result

    result= [array([[ 1.        ,  0.75      , 69.5198822 ],
            ...,
            [99.        , 29.25      , 49.51967621]]),
                       ...
     array([[ 1.        ,  0.75      , 78.88569641],
            ...,
            [99.        , 29.25      , 58.88546753]]),
                       ...
     array([[ 1.        ,  0.75      , 77.7075882 ],
            ...,
            [99.        , 29.25      , 57.70734406]])]

我的字典也被命名為dicti

    {'sub_arr1': array([[37.        ,  2.25      ,  2.62501693],
            ...,
            [81.68138123, 29.25      , 99.        ]]),
                          ...
     'sub_arr2': array([[41.        ,  2.25      ,  2.254704  ],
            ...,
            [85.93613434, 29.25      , 99.        ]]),
                          ...
     'sub_arr3': array([[51.        ,  2.25      ,  1.13132851],
            ...,
            [96.60651398, 29.25      , 99.        ]])}

然后,我想在存儲在這兩個數據集( resultdicti )中的數組之間進行計算。 我想刪除一些比閾值(例如 10)更接近dicti數組的result數組的數據點,並將它們存儲為一個新的。 實際上,我想將第一個result數組與第一個dicti數組進行dicti ,第二個與第二個數組進行dicti ,第三個與第三個數組進行dicti ,依此類推。 我可以說我想清理存儲在result列表中的數組,然后將新的清理數組作為單獨的數組保存在 3d numpy 數組中,或者再次保存一個清理過的數組列表。 我的代碼(不起作用!!!)如下:

import numpy as np    
new_result=np.array ([])
    for i in result:
        for key, value in dicti():
            clp=result[i][np.where(np.min(distance.cdist(value, result[i]),axis=0)<10)[0],:] # it finds the points of result that are closer 10 to points stored as values of dicti
            cleaned_result= npi.difference(result[i], clp) # it removes that close points
            new_result=np.append (new_result, cleaned_result).

我面臨的錯誤是TypeError: only integer scalar arrays can be converted to a scalar index但我認為我的代碼還有其他問題。 在此之前,我感謝任何反饋。

嘗試這樣的事情:

import numpy as np    
new_result=np.array ([])
for res, value in zip(result, dicti.values()):
    clp=res[np.where(np.min(distance.cdist(value, res),axis=0)<10)[0],:] # it finds the points of result that are closer 10 to points stored as values of dicti
    cleaned_result= npi.difference(res, clp) # it removes that close points
    new_result=np.append (new_result, cleaned_result).

zip是一個函數,它接受兩個或多個迭代器,並在每次迭代中返回一個元組,其中每個元素都取自每個迭代器。

例如:

a = [1,2,3]
b = {'A':'a', 'B':'b', 'C':'c'}
for x, y in zip(a, b.values()):
     print(x, y)

這顯示為輸出:

1 a
2 b
3 c

zip完全重現您所說的內容:

我想將第一個結果數組與第一個 dicti 數組進行比較,第二個與第二個數組進行比較,第三個與第三個數組進行比較,依此類推

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM