Python: how to find common values in three lists

前端 未结 3 493
情歌与酒
情歌与酒 2020-12-29 07:42

I try to find common list of values for three different lists:

a = [1,2,3,4]
b = [2,3,4,5]
c = [3,4,5,6]

of course naturally I try to use t

3条回答
  •  一向
    一向 (楼主)
    2020-12-29 08:07

    For those still stumbling uppon this question, with numpy one can use:

    np.intersect1d(array1, array2)
    

    This works with lists as well as numpy arrays. It could be extended to more arrays with the help of functools.reduce, or it can simply be repeated for several arrays.

    from functools import reduce
    reduce(np.intersect1d, (array1, array2, array3))
    

    or

    new_array = np.intersect1d(array1, array2)
    np.intersect1d(new_array, array3)
    

提交回复
热议问题