Python 提取两个列表的交集

来源:互联网 发布:济南锋刃网络 编辑:程序博客网 时间:2024/05/29 06:26

比如已有列表

[1, 2, 3, 4, 5]

[4, 5, 6, 7, 8]

希望能得到二者共同元素[4, 5]的结果?可以用list comprehension方法提取:

>>> A = [1, 2, 3, 4, 5]>>> B = [4, 5, 6, 7, 8]>>> [x for x in A if x in B][4, 5]

原文链接:http://www.lfhacks.com/tech/duplicate-elements-python

0 0
原创粉丝点击