【发布时间】:2021-09-02 10:57:50
【问题描述】:
我面临以下问题。我在 Python 中有多个列表,我希望它们使用某种索引对它们进行排序,以便从其他列表中删除项目。让我进一步解释。
listA_ID = [1,2,3,5,6,7] # integer from 0-250
listA_Var1 = [3.9, 4.7, 2.1, 1.2, 0.15, 0.99]
listB_ID = [2,5,6,7,8,10] # integer from 0-250
listB_Var1 = [0.54, 0.35, 1.19, 2.45, 3.1, 1.75]
>> After Comparison of listA_ID & listB_ID I should end up with the common IDs.
listA_ID = listB_ID = sorted(list(set(listA_ID) & set(listB_ID)))
listA_ID = [2,5,6,7]
listB_ID = [2,5,6,7]
因此,我想从 listA_ID 中删除位于该列表 [0, 2] 位置的元素 [1, 3],并从 listA_Var1 中删除相同的元素,删除位于相同位置的 [3.9, 2.1] [0, 2]。
同样,我想从 listB_ID 中删除位于该列表 [4, 5] 位置的元素 [8, 10] 和 listB_Var1 中的相同内容,删除相同的 [3.1, 1.75]位置 [4, 5]。
>> and then listA_Var1 & listB_Var1 will become
listA_Var1 = [4.7, 1.2, 0.15, 0.99]
listB_Var1 = [0.54, 0.35, 1.19, 2.45]
关于有效实施该方法的任何想法?根据我大量使用 Matlab 的经验,在比较两个列表之后,我有办法获取不需要的索引,然后将这些索引应用于列表,我得到的是最终列表 listA_Var1 和 listB_Var1。
有什么想法吗?提前致谢!
【问题讨论】:
-
这个问题的
pandasnumpy方面是什么? -
我对“pandas”这个词有点陌生(老实说),但是,我认为 numpy 可能是 Matlab 风格的解决方案。
标签: python numpy sorting lambda