有没有更简单的方法来在python的字符串列表中搜索模式?

时间:2019-06-26 15:07:33

标签: python string-matching

我有一个字符串列表,需要一个更好的方法来搜索该列表中的模式。清单的一个例子:

['red','green','red','red','red','red','green','red','red','green','green','red','green','green','red','red','green','green','green','green','green','green','green','red','red','red','red','red','red','green','red','red','red','red','red','red','red','green','green','red','red','green','red','green','green','green','green','green','red','red','green','green','green','red','green','red','green','red','red','green','green','red','green','green','red','red','green','green','red','red','green','green','green','green','red','red','red','red','red','green','green','green','green','red','green','red','green','red','green','red','red','green','red','green','red','green','red','red','red','red','green','red','red','red','green','green','green','red','red','green','green','red','green','red','green','red','green','green','green']

和模式:

BLUE PATTERN:
['red','green','green','green']
['green','red','red','red']

PINK PATTERN:
['red','green','green','red']
['green','red','red','green']

该方法需要扫描我的列表中的模式,并生成另一个带有模式名称的列表,以使模式显示为例:

['blue','pink','blue','blue',..]

那就是我所拥有的:

catalogacao = []

if len(self._items) < 4:
    return
for i, _ in enumerate(self._items):
    if i + 4 > len(self._items):
        break
    if self._items[i] == "red" and self._items[i+1] == "green" and self._items[i+2] == "green" and self._items[i+3] == "green":
        catalogacao.append("blue")

    if self._items[i] == "green" and self._items[i+1] == "red" and self._items[i+2] == "red" and self._items[i+3] == "red":
        catalogacao.append("blue")

    if self._items[i] == "red" and self._items[i+1] == "green" and self._items[i+2] == "green" and self._items[i+3] == "red":
        catalogacao.append("pink")

    if self._items[i] == "green" and self._items[i+1] == "red" and self._items[i+2] == "red" and self._items[i+3] == "green":
        catalogacao.append("pink")

3 个答案:

答案 0 :(得分:0)

您可以遍历所有项目,对于每个包含4个元素的子列表,请检查该子列表是否等于蓝色或粉红色图案

items = ['red','green','red','red','red','red','green','red','red','green','green','red','green','green','red','red','green','green','green','green','green','green','green','red','red','red','red','red','red','green','red','red','red','red','red','red','red','green','green','red','red','green','red','green','green','green','green','green','red','red','green','green','green','red','green','red','green','red','red','green','green','red','green','green','red','red','green','green','red','red','green','green','green','green','red','red','red','red','red','green','green','green','green','red','green','red','green','red','green','red','red','green','red','green','red','green','red','red','red','red','green','red','red','red','green','green','green','red','red','green','green','red','green','red','green','red','green','green','green']

blue_patterns = [['red','green','green','green'], ['green','red','red','red']]

pink_patterns = [['red','green','green','red'], ['green','red','red','green']]

catalogacao = []

#Iterate over the list
for idx in range(len(items)):

    #Check if the 4 element sublist match blue or pink pattern
    if any(item == items[idx:idx+4] for item in blue_patterns):
        catalogacao.append("blue")

    elif any(item == items[idx:idx+4] for item in pink_patterns):
        catalogacao.append('pink')

print(catalogacao)

输出将为

['blue', 'pink', 'pink', 'pink', 'pink', 'blue', 'blue', 'blue', 'pink', 
'pink', 'blue', 'pink', 'blue', 'pink', 'pink', 'pink', 'pink', 'pink', 
'pink', 'blue', 'blue', 'blue', 'pink', 'blue', 'blue', 'blue', 'pink', 
'pink', 'blue']

答案 1 :(得分:0)

这更干净,更简单了:)

pink = [['red','green','green','green'], ['green','red','red','red']]
blue = [['red','green','green','red'], ['green','red','red','green']]
catalogacao = []

for i in range(len(_items) - 4):
    colors = [x for x in _items[i:i+4]]
    if colors in blue:
        catalogacao.append("blue")
    elif colors in pink:
        catalogacao.append("pink")

询问是否有任何问题! :)

答案 2 :(得分:0)

为什么不使用正则表达式?

我们首先将您的模式以更加机器友好的格式组合在一起,然后将它们编译为正则表达式:

patterns = (
    (['red','green','green','green'], 'blue'),
    (['green','red','red','red'], 'blue'),
    (['red','green','green','red'], 'pink'),
    (['green','red','red','green'], 'pink'),
)

pattern_dict = {
    ' '.join(words): out  for words, out in patterns
}

pattern_re = re.compile('|'.join(pattern_letters.keys()))

然后我们可以将其应用于您的商品:

items = ['red','green','red','red','red','red','green','red','red','green','green','red','green','green','red','red','green','green','green','green','green','green','green','red','red','red','red','red','red','green','red','red','red','red','red','red','red','green','green','red','red','green','red','green','green','green','green','green','red','red','green','green','green','red','green','red','green','red','red','green','green','red','green','green','red','red','green','green','red','red','green','green','green','green','red','red','red','red','red','green','green','green','green','red','green','red','green','red','green','red','red','green','red','green','red','green','red','red','red','red','green','red','red','red','green','green','green','red','red','green','green','red','green','red','green','red','green','green','green']

output = []
for match in pattern_re.finditer(' '.join(items)):
    pat = match.group(0)
    output.append(pattern_dict[pat])