【问题标题】:compare to list of string without order in python与python中没有顺序的字符串列表进行比较
【发布时间】:2021-07-24 09:15:24
【问题描述】:

我有一个文件列表,例如:

list1 = ['z.txt','x.txt','c.txt',.....'p.txt']

然后脚本(做一些工作)将创建一个list1的文件列表,没有顺序 我想将这两个与== 进行比较,但是因为list2 不是list1 的顺序,所以脚本没有做任何事情 如何比较这两个列表?

【问题讨论】:

  • 按名称排序列表?或创建一个哈希表
  • list2 是什么?它的内容是什么?
  • 尝试使用集合intersection,例如:"Match" if set(list1).intersection(list2) else "No Match"
  • 什么是list2
  • 您应该以list2 为例说明预期的输出。

标签: python list compare


【解决方案1】:

可能是这样的

def all_elements_match(list1, list2):
    if len(list1) != len(list2):
        return False
    return all([e in list2 for e in list1])

【讨论】:

    猜你喜欢
    • 2021-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多