如何从另外两个包含公共数字对(序列)的数组中创建一个数组?

时间:2018-07-18 10:20:53

标签: python

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

最终结果应该是

c = [6,8,4,5] 

此数组在两个数组中都包含同一对数字-如何在python中编写这种代码?

我只知道如何创建具有重复值的数组

a = [1,2,3,4,5,6,8]
b = [6,8,9,4,5,3,2,1]
c = [x for x in a if x in b]
print (c)

1 个答案:

答案 0 :(得分:5)

>>> [e for t in [t for t in zip(b,b[1:]) if t in zip(a,a[1:])] for e in t]
[6, 8, 4, 5]