2017年8月12日 星期六

[ Python 常見問題 ] How to find list intersection?

Source From Here
Question
How can we achieve a boolean AND operation (list intersection) on two lists?

How-To
If order is not important and you don't need to worry about duplicates then you can use set intersection:



For example:
>>> a = [1,2,3,4,5]
>>> b = [1,3,5,6]
>>> list(set(a) & set(b))
[1, 3, 5]


沒有留言:

張貼留言

[ Py DS ] Ch1 - IPython: Beyond Normal Python

Source From  Here   Keyboard Shortcuts in the IPython Shell   If you spend any amount of time on the computer, you’ve probably found a u...