我有一个清单input
:
[1, 2, 4, 3, 5, 7, 5, 3, 8, 3, 8, 5, 8, 5, 9, 5, 7, 5, 7, 4, 9, 7, 5, 7, 4, 7, 4, 7, 8, 9, 7, 5, 7, 5, 4, 9, 3, 4, 8, 4, 8, 5, 3, 5, 4, 7, 3, 7, 3, 1, 2, 7, 1, 7, 2, 1]
我需要检查 a 的元素是否lookup_list
[1,2,3,4,5,7,8,9,5,4,3,2,1]
以相同的顺序以分散的方式出现在上面的列表中。
这将说明我想说的:
[1, 2, 4, 3, 5, 7, 5, 3, 8, 3, 8, 5, 8, 5, 9, 5, 7, 5, 7,4, 9, 7, 5, 7, 4, 7, 4, 7,8, 9, 7, 5, 7, 5, 4, 9, 3, 4, 8, 4, 8, 5, 3, 5, 4, 7, 3, 7, 3, 1,2, 7, 1, 7, 2,1]
粗体数字是来自lookup_list
存在于相同的订单 in the input
列表,但中间还有其他不相关的项目。
有什么方法可以检查这个吗?
这是我尝试过的方法:
count = 0
a = 0
indices = []
for item in list:
idx = -1
if count < len(input_list):
idx = list.index(input_list[count])
if idx != -1:
a = a +len(list[:idx])
list = list[idx:]
indices.append(a + idx)
count = count +1
print(indices)
但它给了我以下结果:
[0, 2, 5, 35, 25, 24, 33, 30, 33, 37, 38, 64, 54]
问题是,顺序lookup_list
在此方法中不进行维护。