简体   繁体   English

如何从python的超级列表的子列表中找到可能的有序组数?

[英]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. 我想找出AB中保持A的顺序出现了多少次。 For this example A appears 3 times in B . 对于此示例, AB中出现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.

相关问题 Python:从有序列表中对键的子列表进行排序的最快方法是什么 - python: what is the fastest way to sort a sublist of key from an ordered list 在python中,如何通过匹配原始列表中的字符串模式从字符串列表中提取子列表 - In python, how do i extract a sublist from a list of strings by matching a string pattern in the original list 如何在不创建子列表的情况下拆分python列表元素 - How do i split a python list element without creating a sublist 如何检查列表(Python)中是否存在子列表的某些元素? - How do I check if some elements of sublist exists in list (Python)? Python如何查找列表中是否有数字,而不是列表的最后一个数字? - Python How do I find if an number is in a list but is not last number of the list? 如何在python中将数字和列表拆分为子列表 - How to split number and list into sublist in python Python - 如何查找列表(或子列表)中存在的项目? - Python - How to find an item exists in a list (or sublist)? 如何在列表的子列表中查找项目的索引(python) - How to Find the Index of an Item in a sublist of a list (python) python 列表理解:使用列表理解查找子列表中的最大数字 - python list comprehension :find largest number in sublist using list comprehension 在 python 中找不到带有子列表的列表的最大数量 - can't find max number of a list with sublist in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM