问题描述
我需要寻找匹配点"列表"a"中的两个大于列表"b".到目前为止,我已经找到了这篇较早的文章:检查列表是否在保留列表顺序的同时是另一个列表的一部分
I need to seek the "matchingpoint" of two list where List "a" is bigger than List "b".So far I've found this earlier post:Check if a list is part of another list while preserving the list sequence
那很有帮助.但是我现在需要列表适合的地方.
That helps a lot. But I need to now, where the lists fit.
a = [3, 4, 1, 2, 4, 1, 5]
b = [4, 1, 2]
"b"适合a"但我希望将 a [1]
作为第一个匹配点,而不是 TRUE
值
"b" fits in "a" but instead of a TRUE
value I would like to have a[1]
as first matchingpoint
推荐答案
您可以使用下一步以获取第一个匹配点:
You could use next to fetch the first matching point:
a = [3, 4, 1, 2, 4, 1, 5]
b = [4, 1, 2]
starting_point = next((a[i] for i in range(len(a)) if b == a[i:i + len(b)]), -1)
print(starting_point)
输出
4
更新
如果同时需要索引和索引处的值,请返回索引而不是值,例如:
If you need both the index and the value at the index, return the index instead of the value, for example:
position = next((i for i in range(len(a)) if b == a[i:i + len(b)]), -1)
print("position", position)
print("starting point", a[position])
输出
position 1
starting point 4
请注意更改,现在是 i
,而不是 a [i]
Note the change, now is i
instead of a[i]
这篇关于检查列表是否是列表保持顺序的一部分并查找位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!