例如:

a = ['The', 'man', 'is', 'eating', 'pear']

eatingpear是连续的
b = ['these', 'are', 'random', 'words', 'but', 'they', 'have', 'pear', 'and', 'eating']

这是一个随机的单词列表,我想检查b中是否有两个连续的单词
我要怎么做
c = ['eating', 'pear']

最佳答案

c = [(x,y) for x, y in zip(a[0:], a[1:]) if x in b and y in b]
print(c)

10-07 20:23