你可以
zip()
列表本身带有偏移量以获得对。然后找到你正在寻找的一对的索引(假设这种情况发生一次,或者你只关心第一个)。然后把清单拼接起来:
test = [3,5,7,1,10,17]
def partition_on_pair(test, pair):
index = next((i for i, n in enumerate(zip(test, test[1:])) if n == pair), len(test))
return test[:index], test[index:]
partition_on_pair(test, (10, 17))
# ([3, 5, 7, 1], [10, 17])
partition_on_pair(test, (10, 19)) # doesn't exist, so you get an empty
#([3, 5, 7, 1, 10, 17], [])
partition_on_pair(test, (5, 7))
#([3], [5, 7, 1, 10, 17])
partition_on_pair(test, (3,5))
#([], [3, 5, 7, 1, 10, 17])