如何在列表中找到已定义的序列?

时间:2015-11-28 11:01:06

标签: python list python-2.7 sequence

例如,我列出了事件:

list = ['A','B','A','A','B','B','A'...]

我需要找到例如'B','A','A'的事件序列来检查它是否存在。如果是 - 列表中存在多少次。

对于当前列表输出应该是:

Exists: 1 times

4 个答案:

答案 0 :(得分:2)

作为循环和列表切片的练习:

{
  "id":1;
  "name":"aName",
  "created_at":"xxxxxxxx",
  "updated_at":"xxxxxxxx"
}

或者列表理解:

events = ['A','B','A','A','B','B','A','A']
target = ['B', 'A', 'A']

size = len(target)
count = 0
for start in range(len(events) - size + 1):
    if events[start:start + size] == target:
        count += 1

答案 1 :(得分:2)

使用排序后缀数组和二进制搜索:

from bisect import bisect_left

# returns insertion index
def binary_search(seq, el, lo=0, hi=None):
    hi = hi if hi is not None else len(seq)
    pos = bisect_left(seq, el, lo, hi)
    return pos

def subSequenceCount(sequence, subSequence):
    suffixes = []
    for start in range(len(sequence)-1):
        suffixes.append(sequence[start:])
    suffixes.sort()
    insertPos = binary_search(suffixes, subSequence)
    count = 0
    while insertPos < len(suffixes) and suffixes[insertPos][:len(subSequence)] == subSequence:
        count += 1
        insertPos += 1
    return count

答案 2 :(得分:1)

您可以使用正则表达式

>>> import re
>>> l = ['A','B','A','A','B','B','A']
>>> pat = re.compile(r'BAA')
>>> sequences = pat.findall(''.join(l))
>>> sequences
['BAA']
>>> len(sequences)
1
>>> 

但最好的方法是使用生成器函数:

>>> def find_sequences(sequences, events):
...     i = 0
...     events_len = len(events)
...     sequences_len = len(sequences)
...     while i < sequences_len:
...             if sequences[i:i+events_len] == events: 
...                 yield True
...             i = i + 1
... 
>>> list(find_sequences(lst, events))
>>> sum(find_sequences(['AB', 'A', 'BA', 'A', 'BA'], ['A', 'BA']))
2

答案 3 :(得分:1)

如果您可以将列表转换为字符串,则可以使用str.count

lst = ['A','B','A','A','B','B','A']

to_str = ''.join

print "Exists: %d times" % to_str(lst).count(to_str(['B','A','A']))