【问题标题】:How to iterate over arrays stored in a list and dictionary如何迭代存储在列表和字典中的数组
【发布时间】:2021-01-24 12:55:54
【问题描述】:

我想遍历存储在列表和字典中的 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)中的数组之间进行计算。我想删除result数组的一些数据点,这些数据点比dicti数组的阈值(例如10)更接近,并将它们存储为新数组。其实我想比较result的第一个数组和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,但我认为我的代码还有其他问题。 提前感谢任何反馈。

【问题讨论】:

    标签: python arrays numpy dictionary


    【解决方案1】:

    试试这样的:

    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的第一个数组,第二个和第二个,第三个和第三个等等

    【讨论】:

    • 亲爱的@Jorge Morgado,我非常感谢您的帮助。目前,new_result 是所有迭代的 numpy 数组。你知道分离每次迭代的方法吗?我的意思是类似于我的主要列表(result),但它是一个 numpy 数组。如果无法将它们存储为 numpy 数组,是否可以将每次迭代的结果保存为列表的单独组件?提前致谢。
    • 对不起,我还是不明白你说的“分离每个迭代”是什么意思,你能解释的更详细一点吗?
    • 我的主要列表名为 result 有例如三个数组,我的 dictionary.values 也是三个。代码将每个清除的数组一个接一个地添加,但我希望有单独的数组,如result 的数组。 for 循环的每次迭代都会根据字典值清理一个 result 数组。例如,result 是三个不同大小的数组(如 100x3、50x3 和 80x3)的列表。通过清理它(例如删除一半的点),我希望将一个作为 numpy 数组的列表,其大小为 50x3、25x3 和 40x3,或者最好是我想要一个具有这三个的 3d numpy 数组。
    • 所以基本上你想要的是将数组 result 修改为 numpy 数组?
    • 作为一个numpy数组,每次迭代的结果都单独存储,或者像原始列表一样具有单独数组的列表。关键是我想将数组分隔为列表的成员(如原始result)或将它们保留为 numpy 数组的子组件。关键是我不想混合它们并将它们完全放在一个例如 105x3 numpy 数组中(由清理过的数组制成:50x3、25x3 和 40x3,我从我之前的评论中假设它们)。或三个数组的列表,第一个是 50x3,第二个是 25x3,第三个是 40x3。
    猜你喜欢
    • 2019-09-20
    • 2021-02-02
    • 1970-01-01
    • 2016-06-17
    • 2021-08-19
    • 1970-01-01
    • 2017-04-01
    • 2012-07-15
    • 2020-04-01
    相关资源
    最近更新 更多