【问题标题】:Strictly compare all elements of two lists in python without considering their order严格比较python中两个列表的所有元素而不考虑它们的顺序
【发布时间】:2016-12-31 08:21:33
【问题描述】:

如何比较列表之间的每个值。应该是完全匹配的。

list1 = ['same1', 'same2']
list2 = ['notsame1', 'notsame2']
list3 = ['same1', 'same2']

def comp(list1, list2, list3):
    if set(list1) & set(list2):
        print  "Train A is active and instances are %s." % list2
    elif set(list1) & set(list3):
        print  "Train B is active and instances are %s." % list3
    else :
        print "No Trains are active"
comp(list1, list2, list3)

尽管我在 list3 = ['not_same1', 'same2'] 中有一个不同的值,但上面的代码仍将 Train B 返回为活动状态

看起来它只是从列表中检查一个值,即使元素没有顺序,我如何比较所有元素。

看起来**set** 已被弃用,欢迎使用其他任何方式。

【问题讨论】:

  • 为了比较值,我想你想要==。从您的问题来看,您期望的输出并不完全清楚。
  • set() 不会被弃用。 Python2 是。
  • “看起来set 正在被弃用”我没有听说过,我认为它永远不会发生,至少不会很快
  • 澄清一下,你想让['same2', 'same1'] 等于['same2', 'same1']['same1', 'same2'],还是应该['same2', 'same1'] 不等于['same1', 'same2']?您的问题对此并不完全清楚,至少对我而言。
  • list3 不包含'not_same1'

标签: python list set


【解决方案1】:

您可以直接与== 比较集合。由于集合会删除多个值,因此请先检查两个列表的长度是否相同,然后再尝试转换为集合:

from __future__ import print_function


def comp(list1, list2, list3):
    if len(list1) == len(list2) and set(list1) == set(list2):
        print("Train A is active and instances are %s." % list2)
    elif len(list1) == len(list3) and set(list1) == set(list3):
        print("Train B is active and instances are %s." % list3)
    else :
        print("No Trains are active")

运行一些测试:

def test(list1, list2, list3):
    print('#' * 10)
    print('list1', list1)
    print('list2', list2)
    print('list3', list3)
    comp(list1, list2, list3)

list1 = ['same1', 'same2']
list2 = ['notsame1', 'notsame2']
list3 = ['same1', 'same2']
test(list1, list2, list3)
list2 = ['same1', 'same2']
test(list1, list2, list3)
list2 = ['same1', 'same2', 'same2']
test(list1, list2, list3)
list2 = ['notsame1', 'notsame2']
list3 = ['same1', 'same2', 'same2']
test(list1, list2, list3)

输出:

##########
list1 ['same1', 'same2']
list2 ['notsame1', 'notsame2']
list3 ['same1', 'same2']
Train B is active and instances are ['same1', 'same2'].
##########
list1 ['same1', 'same2']
list2 ['same1', 'same2']
list3 ['same1', 'same2']
Train A is active and instances are ['same1', 'same2'].
##########
list1 ['same1', 'same2']
list2 ['same1', 'same2', 'same2']
list3 ['same1', 'same2']
Train B is active and instances are ['same1', 'same2'].
##########
list1 ['same1', 'same2']
list2 ['notsame1', 'notsame2']
list3 ['same1', 'same2', 'same2']
No Trains are active

【讨论】:

    【解决方案2】:

    使用相等检查set(list1)==set(list2) 而不是交集set(list1) & set(list2)

    【讨论】:

      【解决方案3】:

      只需使用== 即可满足您的需求。它直接比较两个序列的值:

      list1 = ['same1', 'same2']
      list2 = ['notsame1', 'notsame2']
      list3 = ['same1', 'same2']
      
      def comp(list1, list2, list3):
          if list1 == list2:
              print(  "Train A is active and instances are %s." % list2)
          elif list1 == list3:
              print(  "Train B is active and instances are %s." % list3)
          else :
              print( "No Trains are active")
      comp(list1, list2, list3)
      

      【讨论】:

        【解决方案4】:

        即使元素没有顺序,我如何比较所有元素。

        这是一种不使用 set 来比较 3 个数组的方法。

        def compareArrays(list1, list2):
            for item in list1:
                if not item in list2:
                    return False # The array's don't match
            return True # The array's match
        
        def comp(list1, list2, list3):
            if compareArrays(list1, list2):
                print(  "Train A is active and instances are %s." % list2)
            elif compareArrays(list1, list3):
                print(  "Train B is active and instances are %s." % list3)
            else:
                print( "No Trains are active")
        
        list1 = ['same1', 'same2']
        list2 = ['notsame1', 'notsame2']
        list3 = ['same1', 'same2']
        
        comp(list1, list2, list3)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-12-25
          • 2017-11-10
          • 2013-07-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-09-08
          • 1970-01-01
          相关资源
          最近更新 更多