我试图找出列表中的值是否存在于另一个列表中。 例如:
list1 = ['a','b']
list2 = ['a','b','c','d','e','a','b','c']
list3 = ['a','a','b','b','b','a','b','a']
我想检查list1
和list2
中是否存在list 3
中的值list2
。但是,当我与list2
进行比较时,检查应该返回false,因为public func jsonPrint(_ o: NSObject, spacing: String = "", after: String = "", before: String = "") {
let newSpacing = spacing + " "
if o.isArray() {
print(before + "[")
if let a = o as? Array<NSObject> {
for object in a {
jsonPrint(object, spacing: newSpacing, after: object == a.last! ? "" : ",", before: newSpacing)
}
}
print(spacing + "]" + after)
} else {
if o.isDictionary() {
print(before + "{")
if let a = o as? Dictionary<NSObject, NSObject> {
for (key, val) in a {
jsonPrint(val, spacing: newSpacing, after: ",", before: newSpacing + key.description + " = ")
}
}
print(spacing + "}" + after)
} else {
print(before + o.description + after)
}
}
}
除了&#39; a&#39;之外还有其他值。 &安培; &#39; B&#39 ;.与list3相比,检查应该返回true,因为它只有&#39; a&#39; b&#39;。任何线索我怎么能这样做?
希望这很清楚。
答案 0 :(得分:4)
使用套装:
>>> set(list1) == set(list2)
False
>>> set(list1) == set(list3)
True
答案 1 :(得分:0)
尝试all
,
In [11]: all(i in list1 for i in list2)
Out[11]: False
In [12]: all(i in list1 for i in list3)
Out[12]: True