lst1 =[11,22,33]
lst2 =[22,33,44]
a =set(lst1) # 转成元祖
b =set(lst2)
c =(a & b) # 集合c和b中都包含了的元素
print('两个列表中相同的元素是:', end='')for i in c:print(i, end=' ')
方法二(while 循环方法):
# while 循环方法
lst1 =[11,22,33]
lst2 =[22,33,44]
i =0 # 索引初始值 =0while i <len(lst1):if lst1[i] in lst2:print(lst1[i], end=' ')else:
pass
i +=1
方法三:(fo循环实现方法)
# fo循环实现方法
lst1 =[11,22,33]
lst2 =[22,33,44]for i in lst1:if i in lst2:print(i, end=' ')# fo循环嵌套实现方法
lst1 =[11,22,33]
lst2 =[22,33,44]for i in lst1:for j in lst2:if i == j:print(i, end=' ')