【发布时间】:2011-05-18 01:42:32
【问题描述】:
def common_elements(list1, list2):
"""
Return a list containing the elements which are in both list1 and list2
>>> common_elements([1,2,3,4,5,6], [3,5,7,9])
[3, 5]
>>> common_elements(["this","this","n","that"],["this","not","that","that"])
['this', 'that']
"""
result = []
for element in list1:
if element in list2:
result.append(element)
return result
到目前为止我有这个,但它返回重复,例如:
common_elements(["this","this","n","that"],["this","not","that","that"])
返回为:['this', 'this', 'that']
【问题讨论】:
标签: python