Check if two items are in a list, in a particular

2020-02-09 14:43发布

Say I have a list v = [1, 2, 3, 4, 3, 1, 2]. I want to write a function, find_pair which will check if two numbers are in the list and adjacent to each other. So, find_pair(v, 2, 3) should return True, but find_pair(v, 1, 4) should return False.

Is it possible to implement find_pair without a loop?

标签: python list
13条回答
爷、活的狠高调
2楼-- · 2020-02-09 15:29
v = [1,2,3,4,3,1,2]

def find(x,y,v):
        return (x,y) in zip(v,v[1:])

print find(2,3,v)
print find(1,4,v)
查看更多
登录 后发表回答