与 python 中没有顺序的字符串列表进行比较
compare to list of string without order in python
我有一个文件列表,例如:
list1 = ['z.txt','x.txt','c.txt',.....'p.txt']
然后脚本(做一些工作)将创建 list1
的文件列表,没有顺序
我想将这两个与 ==
进行比较,但是因为 list2
不是 list1
的顺序,所以脚本没有做任何事情
我如何比较这两个列表?
可能是这样的
def all_elements_match(list1, list2):
if len(list1) != len(list2):
return False
return all([e in list2 for e in list1])
我有一个文件列表,例如:
list1 = ['z.txt','x.txt','c.txt',.....'p.txt']
然后脚本(做一些工作)将创建 list1
的文件列表,没有顺序
我想将这两个与 ==
进行比较,但是因为 list2
不是 list1
的顺序,所以脚本没有做任何事情
我如何比较这两个列表?
可能是这样的
def all_elements_match(list1, list2):
if len(list1) != len(list2):
return False
return all([e in list2 for e in list1])