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]


沒有留言:

張貼留言

[ Python 文章收集 ] List Comprehensions and Generator Expressions

Source From  Here   Preface   Do you know the difference between the following syntax?  view plain copy to clipboard print ? [x  for ...