猫史档案馆


python试题,你最少用几行代码

用户:1111111111111111211111111111111112查看:2 回复:2 评论:2 创建时间:2023-05-02T20:13:30


 

 

试题:

写一个函数,接收两个列表作为参数,返回两个列表中所有的共同元素(即交集)。

要求:不能使用Python内置的集合(set)类型或相关的集合操作。

示例输入:

[1, 2, 3, 4, 5], [3, 4, 5, 6, 7]

示例输出:

[3, 4, 5]

 

本人用了10行代码


回复

上一页1 页 / 共 1下一页
1111111111111111211111111111111112

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


评论


SCS_user_LoeheodVOQSCS_user_LoeheodVOQ

func = lambda l1, l2: list(filter(lambda i: i in l1, l2))

一行

点赞0


评论