x =[1,2,3,4,5,6]
y = [1,2,3,4,5]
if x == y:
print("Numbers found")
else:
print("Numbers not found")
我想列印串列 y 中不存在的數字。
uj5u.com熱心網友回復:
最快的方法是在集合中轉換并列印差異:
>>> print(set(x).difference(set(y)))
{6}
此代碼列印存在x但不存在的數字y
uj5u.com熱心網友回復:
你可以這樣做:
x = [1,2,3,4,5,6]
y = [1,2,3,4,5]
for i in x:
if i not in y:
print(i)
uj5u.com熱心網友回復:
x =[1,2,3,4,5,6]
y = [1,2,3,4,5]
for i in x:
if i in y:
print(f"{i} found")
else:
print(f"{i} not found")
uj5u.com熱心網友回復:
這是我認為的最佳選擇。
x =[1,2,3,4,5,6]
y = [1,2,3,4,5]
for number in x:
if number not in y:
print(f"{number} not found")
uj5u.com熱心網友回復:
得到不匹配:
def returnNotMatches(a, b):
return [[x for x in a if x not in b], [x for x in b if x not in a]]
或者
new_list = list(set(list1).difference(list2))
獲得交點:
list1 =[1,2,3,4,5,6]
list2 = [1,2,3,4,5]
list1_as_set = set(list1)
intersection = list1_as_set.intersection(list2)
輸出:
{1, 2, 3, 4, 5}
您還可以將其轉移到串列中:
intersection_as_list = list(intersection)
或者:
new_list = list(set(list1).intersection(list2))
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/419212.html
標籤:
