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 文章收集 ] SQLAlchemy quick start with PostgreSQL

Source From   Here   Preface   This is a quick tutorial for getting started with   SQLAlchemy Core API .   Prerequisites   In this quick st...