[英]How do I find the number of ordered group possible as in a sublist from a super list in python?
Suppose I have a smaller list A = [1,2,3]
and larger list B = [1,2,3,1,1,2,2,3,2,3]
. 假设我有一个较小的列表A = [1,2,3]
和一个较大的列表B = [1,2,3,1,1,2,2,3,2,3]
。
B has no other elements except A 's but elements order is not maintained. 除了A之外, B没有其他元素,但是不保持元素顺序。
I want to find how many times A appears in B preserving A 's order. 我想找出A在B中保持A的顺序出现了多少次。 For this example A appears 3 times in B . 对于此示例, A在B中出现3次。 I could solve this for two elements, like [1,2]
comes 2 times in [1,1,2,2,1,1]
. 我可以用两个元素来解决这个问题,例如[1,2]
在[1,1,2,2,1,1]
出现2次。 In other words, I want to find how many ordered group such A is possible from the larger list as B . 换句话说,我想从较大的列表B中找到多少个有序的组,例如A。
From what I understood, you want to count how many times all the elements of A are repeated in order in B , even if there are other elements inbetween. 据我了解,您希望计算A的所有元素按B顺序重复多少次,即使它们之间还有其他元素也是如此。
If that's the case, you can use: 如果是这样,您可以使用:
A = [1,2,3]
B = [1,1,1,1,1,1,1,1,1,2,3,1,1,2,2,3,2,3,3,3,3,3,3,3,3]
counters = [0 for _ in A] # initialize a list with the same number of values of A, but all at 0
for x in B: # for each element in B
for n in range(len(A)): # for all the indexes in A
if x == A[n]: # if the element in B is present in A
if n == 0 or (counters[n] < counters[n-1]):
# if n == 0, is the first element of A: we know it's a start of a possible match
# if the previous number in index is higher of the current number, means that we are looking for the match to continue
counters[n] += 1 # add 1 to the current number
break
print counters[-1] # the last number of the counters represent the times that you reached the end of a match
An efficient approach is to build a dict of queues of indices for each item in B
, then cycle through items in A
to look for the next item in the dict whose index is greater than the index of the last found item by keeping dequeueing until such an index is found or break the loop if any queue is exhausted, with each completed cycle incrementing the count by 1: 一种有效的方法是为B
每个项目建立一个索引队列的字典,然后在A
项目之间循环,以通过保持出队直到该字典中的索引大于上次找到的项目的索引来查找下一个项目。如果发现队列已用完,则找到索引或中断循环,每个完成的循环将计数增加1:
from collections import deque
index = {}
for i, n in enumerate(B):
index.setdefault(n, deque()).append(i)
count = 0
while True:
last = -1
try:
for n in A:
while True:
i = index[n].popleft()
if i > last:
last = i
break
except (IndexError, KeyError):
break
count += 1
count
becomes: count
变为:
3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.