【问题标题】:How to loop to check if all values in a list are bigger than the values in another list? [duplicate]如何循环检查列表中的所有值是否大于另一个列表中的值? [复制]
【发布时间】:2021-06-04 17:57:48
【问题描述】:

正如问题所暗示的,我如何执行循环来检查列表中的所有值是否大于另一个列表中的另一组值?假设每个列表的长度相同,并且每个值将根据其索引与另一个值进行比较。

例如:

aList=[1,5,10,15,23]
bList=[0,4,9,14,22]
for x in range(len(aList)):
   if aList[x]>bList[x]:
      print("All values in aList are bigger than bList")
   else:
      print("Not all values in aList are bigger than bList")

这是我现在所拥有的,但由于循环,我最终打印了 5 次。我只希望结果打印一次。有谁知道解决这个问题的方法吗?

【问题讨论】:

  • @AbdulAzizBarkat 如果你仔细阅读,我认为他正在寻找每个元素的成对比较

标签: python list loops indexing


【解决方案1】:

使用allzip

if all(a > b for a, b in zip(aList, bList)):
    print("All values in aList are bigger than bList")
else:
    print("Not all values in aList are bigger than bList")

【讨论】:

    【解决方案2】:

    使用 numpy:

    import numpy 
    
    if np.all(np.array(aList) > np.array(bList)):
        print("All values in aList are bigger than bList")
    else:
        print("Not all values in aList are bigger than bList")
    

    【讨论】:

    • 在我看来,使用 numpy 来完成这样一个微不足道的任务将是矫枉过正
    • 对于类似数组的大型操作,numpy 是唯一的答案:)
    • @Vishnudev 我不知道您认为这会以何种方式过度杀伤力。 Numpy 很容易获得,得到很好的支持,并且在数组操作方面比原生 python 性能要好得多。
    • otoh... 我会为您的zip + allsolution 获取一组琐碎(
    • Numpy 不仅对这个问题有点矫枉过正,它也比标准库慢。我用@Vishnudev 的stdlib 解决方案和这个解决方案进行了计时实验。该解决方案在最大 10^9 的最坏情况测试用例上始终花费两倍的时间。 @Vishnudev 解决方案的另一个优点是它可以利用生成器来处理大问题,而这个解决方案会将任何生成器具体化为np.array,对于非常大的情况,这可能会导致主机交换。
    【解决方案3】:

    采用您所做的方法,只需设置一个标志变量,然后在循环后检查标志并打印结果。下面是实现

    aList=[1,5,10,15,23]
    bList=[0,4,9,14,22]
    for x in range(len(aList)):
       if aList[x] < bList[x]:
          flag = True
          break
    if flag:
        print('bList is bigger')
    else:
        print('aList is bigger')
    

    【讨论】:

      【解决方案4】:

      您只需要对您的代码进行细微调整。不是在每个元素上打印,而是检查条件并在最后根据布尔变量的值打印,在这种情况下是 eachGreater

      aList=[1,5,10,15,23]
      bList=[0,4,9,14,22]
      
      eachGreater = True
      
      for x in range(len(aList)):
          if aList[x]>bList[x]:
              pass
          else:
              eachGreater = False
              break
      
      if eachGreater:
          print("All values in aList are bigger than bList")
      else:
          print("Not all values in aList are bigger than bList")
      

      【讨论】:

        【解决方案5】:

        试试这个:

        aList=[1,5,10,15,23]
        bList=[0,4,9,14,22]
        aisbigger=True
        for x in aList:
           for y in bList:
              if(x<y):
                 aisbigger=False
        if(aisbigger):
           print("All values in aList are bigger than bList")
        else:
           print("Not all values in aList are bigger than bList")
        

        编辑后: 如果我没有错,您只想将每个元素与另一个列表中的相同索引元素进行比较。然后试试这个:

        aList=[1,5,10,15,23]
        bList=[0,4,9,14,22]
        aisbigger=True
        for x in range(len(aList)):
           if(aList[x]<bList[x]):
                 aisbigger=False
        if(aisbigger):
           print("All values in aList are bigger than bList")
        else:
           print("Not all values in aList are bigger than bList")
        

        【讨论】:

          【解决方案6】:

          Numpy使用模块greater_equal接近

          a=[1,5,10,15,23]
          b=[0,4,9,14,22]
          
          #Approach 1
          
          if np.greater_equal(a,b).all():
              print("All values in aList are bigger than bList")
          else:
              print("Not all values in aList are bigger than bList")
          

          会给你

          aList 中的所有值都大于 bList

          重新运行a=[1,5,10,15,20] 的代码会给你

          并非 aList 中的所有值都大于 bList

          【讨论】:

          • 但我仍然更喜欢 Vishnudev 建议,因为一旦条件为真,它具有短路评估的优势,并且在我的情况下不需要额外导入 library 例如 numpy
          【解决方案7】:

          这是最直接的解决方案。这是对您已有的内容的一个小改编。

          def each_item_larger(a, b):
              for pos in range(len(a)):
                  if (a[pos] < b[pos]):
                      return False
              return True
          
          aList = [1, 5, 10, 15, 23]
          bList = [0, 4, 9, 14, 22]
          
          if each_item_larger(aList, bList):
              print(“Each item is larger.”)
          else:
              print(“At least one item is smaller.”)
          

          【讨论】:

            【解决方案8】:

            这是我的实现:

            isGreater = True
            for x in range(len(aList)):
                if aList[x]<bList[x]:
                    isGreater = False
                    break
            if isGreater:
                print("List A is greater than List B")
            else:
                print("List A is not greater than List B")
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2021-09-19
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2019-05-26
              • 1970-01-01
              • 2021-02-23
              相关资源
              最近更新 更多