【问题标题】:filter list of list by matching with lists in other list通过与其他列表中的列表匹配来过滤列表列表
【发布时间】:2020-07-23 09:45:44
【问题描述】:

我有 2 个列表,我想根据与 list2 中的元素匹配来过滤 list1:

list1 = [[1,2,3], [4,5,3]]
list2 = [[9,8,3], [7,10,6]]

我想从list1(list)中获取list2中元素(位置相同)中最后一个元素相同的元素

在我的例子中

list1[0][-1] is similar to list2[0][-1]

我只想比较相同位置的列表;表示 list1[0] vs list2[0] and list1[1] vs list2[1] and list1[2] vs list2[2] 依此类推(仅列出位置相同)

【问题讨论】:

    标签: python


    【解决方案1】:

    试试这个,

    print([x for x, y in zip(list1, list2) if x[-1] == y[-1]])
    
    [[1, 2, 3]]
    

    【讨论】:

      猜你喜欢
      • 2020-11-13
      • 2020-11-13
      • 1970-01-01
      • 1970-01-01
      • 2017-06-14
      • 2022-01-03
      • 1970-01-01
      • 2023-03-23
      • 2022-01-06
      相关资源
      最近更新 更多