本题目要求读入2个行整数,放在两个列表,然后找出两个列表中共有的元素,放在新的列表中。
输出新列表中的元素。
输入格式:
输入在2行中分别给出若干个整数。
输出格式:
输出共有的元素,每个元素后面有一个空格。
如果没有共有的元素,输出not found!
输入样例:
2 3 8 11 14 16 20
4 7 8 10 14 16
输出样例:
8 14 16
提交代码:
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = [x for x in a if x in b]
if len(c)>0:
print(*c,end=" ") #用*c可以避免打印出列表的括号
else:
print("not found!")
其他想法:
但是顺序会乱,暂时没有想到解决方法
a = list(map(int,input().split()))
b = list(map(int,input().split()))
print(set(a).intersection(set(b)))