list - 在python列表中，如何搜索多个元素？

``````
l = ['a','b','c','d','a','a','b','c','c','d','d','d']

seq = ['a','b','c']

sum(1 for i in range(len(l) - len(seq)-1) if l[i: i + len(seq)] == seq)

# 2

``````

``````
[l[i: i + len(seq)] for i in range(len(l) - len(seq)-1)]

[['a', 'b', 'c'],

['b', 'c', 'd'],

['c', 'd', 'a'],

['d', 'a', 'a'],

['a', 'a', 'b'],

['a', 'b', 'c'],

['b', 'c', 'c'],

['c', 'c', 'd']]

``````

``````
''.join(l).count(''.join(seq))

# 2

``````
``````
abc = ['a', 'a', 'b', 'c', 'd', 'a', 'a', 'b', 'c', 'c', 'd', 'd', 'd']

min([abc.count(i) for i in ['a', 'b', 'c']])

``````

``````
from collections import Counter

l = ['a','b','c','d','a','a','b','c','c','d','d','d']

seq = ['a','b','c']

#Create counter of list

c = Counter(l)

#{'d': 4, 'a': 3, 'c': 3, 'b': 2}

#Take minimum count from counts of keys a,b and c

result = min([c[key] for key in seq])

print(result)

``````

``````
>>> from collections import Counter

>>>

>>> l = ['a','b','c','d','a','a','b','c','c','d','d','d']

>>> counter = Counter(l)

>>>

>>> min(counter['a'], counter['b'], counter['c'])

2

``````

``````
from collections import defaultdict

dic=defaultdict(int)

l=[a,a, b,c,d,a,a,b,c,c,d,d,d]

seq=[a,b,c]

for i in l:

dic[i]+=1

sol = min([dic[i] for i in seq])

print(sol)

``````

c = ['a','b','c'] cc = ['a','a','b','c','d','a','a','b','c','c','d','d','d']

``````
result = {}

for i in c:

res = cc.count(i)

chec_res = {i:res}

result.update(chec_res)

Min_val = min(result.keys(), key=(lambda m: result[m]))

``````