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']
"""
for element in list1:
if element in list2:
return list(element)
到目前爲止,但似乎無法使其工作!由於2個列表之間的通用元素比較
+1而我個人倒用frozenset,因爲它是不可變的,因此可以作爲字典的鍵等 – zebrabox 2010-05-19 11:04:53
這將返回/獨特/共同的元素,但不是可能存在的任何重複的元素。 – Dologan 2014-03-20 18:52:18
@SilentGhost。如何從兩個列表中獲取匹配元素的數量。在這種情況下,它是2。 – Poka 2017-12-09 11:53:38