å¯ä»¥å¯¹ç¬¬äºä¸ªlistçå
ç´ è¿è¡éåï¼æ£æ¥æ¯å¦åºç°å¨ç¬¬äºä¸ªlistå½ä¸ï¼å¦æ使ç¨è¡¨ç解ï¼å¯ä»¥ä½¿ç¨ä¸è¡ä»£ç å®æä»»å¡ã
list1 = [1,2,3,4,5]
list2 = [4,5,6,7,8]
print [l for l in list1 if l in list2]
# [4,5]
å¦ææ¯ä¸ä¸ªå表ä¸å没æéå¤çå
ç´ ï¼é£ä¹è¿æå¦å¤ä¸ç§æ´å¥½çåæ³ãé¦å
æ两个list转æ¢æsetï¼ç¶å对两个setå交éï¼å³å¯å¾å°ä¸¤ä¸ªlistçéå¤å
ç´ ã
set1 = set(list1)
set2 = set(list2)
print set1 & set 2
# {4,5}
温馨提示:答案为网友推荐,仅供参考