简体   繁体   English

检查两个无序列表是否在某些条件下相等

[英]Check if two unordered lists are equal with certain conditions

I'm looking for an easy (and quick) way to determine if two unordered lists contain the same elements(explained in below example):我正在寻找一种简单(快速)的方法来确定两个无序列表是否包含相同的元素(在下面的示例中说明):

['one', 'two', 'three'] == ['one', 'two', 'three'] :  true
['one', 'two', 'three'] == ['one', 'three', 'two'] :  true
['one', 'two', 'three'] == ['one', 'two', 'three', 'four'] :  false
['one', 'two', 'three'] == ['one', 'three', 'four','five'] :  false
['one', 'two', 'three'] == ['one', 'two'] :  true
['one', 'two', 'three'] == ['one'] :  true

Judging by your example you need to check if the second list is a subset of the first list.从您的示例来看,您需要检查第二个列表是否是第一个列表的子集。 You can do this using issubset :您可以使用issubset执行此操作:

a = ['one', 'two', 'three']
b = ['one']

print(set(b).issubset(a))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM