【问题标题】:Python checking if two lists have same value at same indexPython检查两个列表是否在同一索引处具有相同的值
【发布时间】:2020-08-20 07:59:08
【问题描述】:

所以我给了两个列表

lst1 =[0,1,1,1,0]
lst2 =[0,0,1,1,0]

我需要查看两个列表中哪个索引的值为 1,这是我目前的代码

x = list(zip(lst1,lst2))
for i in range(len(x)):
    flag = 0
    for y in range(len(x[i])):
        if x[i][y] == 1:
            flag +=1
    if flag == 2:
        z = x.index(x[i])
        print(z)

但是这会打印索引 2 和 2 而不是 2 和 3 任何人都可以在这里指出问题,谢谢

【问题讨论】:

    标签: python python-3.x list


    【解决方案1】:

    假设它们的长度相同,您可以这样使用:

    >>> [i for i, (x, y) in enumerate(zip(lst1, lst2)) if x == y == 1]
    [2, 3]
    

    【讨论】:

      【解决方案2】:

      关于如何做得更好的许多答案。

      关于您的代码 sn-p,List.index 始终给出列表中的第一个匹配元素。这就是我们是 2,2 的原因

      >>> lst1 =[0,1,1,1,0]
      >>> lst2 =[0,0,1,1,0]
      >>> x = list(zip(lst1,lst2))
      >>> x
      [(0, 0), (1, 0), (1, 1), (1, 1), (0, 0)]
      >>> x.index((1,1))
      2
      >>> x.index((1,1))
      2
      

      【讨论】:

      • 啊,这就是为什么即使在 for 循环中 i = 3 它仍然会打印第一个等于 (1,1) 的值,即索引 2,谢谢
      【解决方案3】:

      您可以执行以下操作:

      lst1 =[0,1,1,1,0]
      lst2 =[0,0,1,1,0]
      assert len(lst1) == len(lst2)
      
      idx = [i for i in range(len(lst1)) if lst1[i] == 1 and lst2[i] == 1]
      
      print(idx)
      

      使用numpy 的其他解决方案是:

      import numpy as np
      
      lst1 =[0,1,1,1,0]
      lst2 =[0,0,1,1,0]
      assert len(lst1) == len(lst2)
      lst1_ = np.array(lst1)
      lst2_ = np.array(lst2)
      
      idx_ = np.intersect1d(np.where(lst1_ == 1)[0],np.where(lst2_ == 1)[0])
      print(list(idx_))
      

      另一种选择是切换以下行:

      idx_ = np.intersect1d(np.where(lst1_ == 1)[0],np.where(lst2_ == 1)[0])
      

      作者:

      idx_ = np.where((lst1_==1)&(lst2_==1))[0]
      

      正如@yatu 所说,是使用按位运算。

      【讨论】:

      • 你有什么理由 zip 他们到 x 然后从不做任何事情吗?
      • @Axe319 不,我只是从问题中复制的,已编辑
      • 在这些情况下按位运算很方便,无需使用索引np.where((lst1_==1)&(lst2_==1))[0]
      • @yatu 你说的很对,我也把它加到我的答案里了
      【解决方案4】:

      您可以在一个循环中迭代值对及其索引:

      for idx, (i, j) in enumerate(zip(lst1, lst2)):
          if i == j == 1:
              print(idx)
      

      【讨论】:

        【解决方案5】:

        您可以使用元素方面的 and 来做到这一点:

        l = [i for i, _ in enumerate(lst1 and lst2) if _ == 1]
        

        使用and 函数,只有在两个列表的元素的值都为 1 的情况下,表达式才会返回 1,这似乎非常适合您的问题。

        【讨论】:

        • 确实如此。请参阅here @marat Btw 通常_ 是不会使用的变量的约定,这里不是这种情况
        • @yatu 链接本身是指向 SO,而不是官方文档。引用帖子中的链接指向 Python 文档,但它没有说明可迭代对象的特殊行为。我对_ 没有任何疑问 - 这是一个众所周知的约定。
        • 它不在文档中,因为它是布尔运算符实际行为的“副作用”。但它是众所周知的。该链接只是对“为什么”的解释。我不知道你说的I did not have any questions about _ - this is a well known convention是什么意思
        • @yatu 好的,我现在明白了。令人困惑的部分:@ccl 答案暗示 and 将按元素应用,这根本不是真的。您将我的问题解释为将 iterables 解释为布尔值 - 这在引用的问题中进行了讨论。我知道这种效果,但将您的话误解为确认@ccl 的观点(这是错误的)
        • @ccl 这只有效,因为lst1 在所有位置都有lst2 有。如果两个列表有很大的不同,甚至它们的值被交换了,很明显and 不能这样工作
        【解决方案6】:

        看,简单地遍历两个列表。假设您知道两个列表的长度,那么只需执行以下操作:

        lst1 =[0,1,1,1,0]
        lst2 =[0,0,1,1,0]
        
        # as we know length of the both lists, and their length are equal, 
        # i'll just write length as 5, but you can have other algorhitm of finding length
        
        list_len = 5
        
        is_there_any_matches = False
        
        for index in range(list_len):
        
            if lst1[index] == lst2[index]:
        
               is_there_any_matches = True
               break                           # to exit after first match
        
        

        请注意,这将在第一次匹配后中断循环,但您可以删除 break,并计算匹配数。此外,始终采用较小列表的长度,以防止脚本因列表索引超出范围而出错。玩得开心!


        编辑

        我试图让它尽可能简单,但您可以使用生成器或其他 Python 技巧来使其更短且更方便使用。

        【讨论】:

          【解决方案7】:

          您也可以使用 NumPy 的元素式 & 操作,以及 argwhere 来实现:

          >>> np.argwhere((np.array(lst1) == 1) & (np.array(lst2) == 1)).ravel()
          array([2, 3])
          

          【讨论】:

            猜你喜欢
            • 2022-11-27
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-02-03
            • 2022-01-03
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多