用户:
11111111111111112查看:2 回复:2 评论:2 创建时间:2023-05-02T20:13:30
试题:
写一个函数,接收两个列表作为参数,返回两个列表中所有的共同元素(即交集)。
要求:不能使用Python内置的集合(set)类型或相关的集合操作。
示例输入:
[1, 2, 3, 4, 5], [3, 4, 5, 6, 7]
示例输出:
[3, 4, 5]
本人用了10行代码
def list_jiaoji(list1,list2):
q=[]
w=len(list1) if len(list1)<len(list2)else len(list2)
for x in range(int(w)):
if list1[x]==list2[x]:
q.append(list1[x])
else:
pass
return q
print(list_jiaoji([1,2,3],[1,2,3]))
本人代码
点赞0
评论