简体   繁体   English

如何在Python中的列表中提取相等的元素序列?

[英]How to extract an equal sequence of elements in a list in Python?

I have a more specific question, but I haven't found the answer yet.我有一个更具体的问题,但我还没有找到答案。 I'm really desperate and would be really happy if someone knew the answer.我真的很绝望,如果有人知道答案,我会很高兴。 Thank you in advance for reading ...提前感谢您阅读...

I have a list in Python that looks something like this:我在 Python 中有一个列表,看起来像这样:

["h", "e", "l", "l", "o", "h", "e", "l", "l", "o", "h", "e", "l", "l", "o"] ["h", "e", "l", "l", "o", "h", "e", "l", "l", "o", "h", "e", " l", "l", "o"]

Now I want to shorten the list so that a block of elements is filtered out that is repeated many times.现在我想缩短列表,以便过滤掉重复多次的元素块。 This means that this list becomes:这意味着该列表变为:

["h", "e", "l", "l", "o"] [“你好”]

Does anyone know how this works?有谁知道这是如何工作的? The problem: The list can always look different, maybe like this:问题:列表看起来总是不同的,可能是这样的:

["b", "y", "e", "b", "y", "e", "b", "y", "e"] ["b", "y", "e", "b", "y", "e", "b", "y", "e"]

Thank you very much and I would really appreciate your answer!非常感谢,非常感谢您的回答!

This can be handled quite neatly with a single line function (see below).这可以用单行函数非常巧妙地处理(见下文)。

import re

def shorten(l):
  return list(re.sub(r'^([a-z]+)\1+$',r'\1', ''.join(l)))


l1 = ["h", "e", "l", "l", "o", "h", "e", "l", "l", "o", "h", "e", "l", "l", "o"]
l2 = ["b", "y", "e", "b", "y", "e", "b", "y", "e"]

print(shorten(l1))
print(shorten(l2))

Output输出

['h', 'e', 'l', 'l', 'o']
['b', 'y', 'e']

Explanation解释

The above solution treats the list ( l ) passed at runtime as a str of characters in index order.上述解决方案将运行时传递的list ( l ) 视为按索引顺序排列的str字符。

It makes use of the regex pattern ^([az]+)\\1+$ to identify whether the whole str is made up of a substring that is repeated - ie is l made up of a single repeating pattern from start to finish?它利用正则表达式模式^([az]+)\\1+$来确定整个str是否由重复的子字符串组成 - 即l由从头到尾的单个重复模式组成?

If this pattern produces a match on the l str , a list representing this repeating pattern (match group 1 ( \\1 ) is returned.如果此模式在l str上产生匹配,则返回表示此重复模式(匹配组 1 ( \\1 ) 的list )。

If no match is made - ie l is not made up entirely of a single repeating pattern - then a list identical to l passed at runtime is returned.如果没有匹配 - 即l不完全由单个重复模式组成 - 则返回与在运行时传递的l相同的list

This is a possible solution:这是一个可能的解决方案:

def shorten(lst):
    s = ''.join(lst)
    for i in range(1, int(len(s) / 2) + 1):
        if len(s) % i == 0:
            if s[0: i] * int(len(s) / i) == s:
                return list(s[0: i])
    return list(s)

Here are some examples:这里有些例子:

>>> shorten(['h','e','l','l','o','h','e','l','l','o','h','e','l','l','o'])
['h', 'e', 'l', 'l', 'o']
>>> shorten(['b','y','e','b','y','e'])
['b', 'y', 'e']
>>> shorten(['a','b','c'])
['a', 'b', 'c']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在序列中找到最长的相等元素序列? - How to find the longest sequence of equal elements in a sequence? 如何从 Python 中的复杂列表中提取元素 - How to extract elements from a complex list in Python Python:确定列表中相等项的序列长度 - Python: determine length of sequence of equal items in list 在python中,如何以重复的随机序列提取不同值的(最小)列表? - In python, how to extract the (minimal) list of different values in a random sequence with repetitions? 如何检查 python 列表中的最后一个元素有多少相等? - How to check how many of the last elements in a list are equal in python? 如何打印列表的元素,其总和等于 python 中的给定数字? - how to print elements of a list whose sum is equal to a given number in python? python从整数列表中为每个'x'元素提取一个热编码序列(滑动窗口) - python from a list of integers extract a one-hot encoded sequence for every 'x' elements (sliding window) 如何使用python对齐和比较列表中的两个元素(序列) - How to align and compare two elements (sequence) in a list using python 如何根据python3中的元素序列在列表中创建子列表? - How to create sublists within a list based on a sequence of elements in python3? 在 python 的列表中查找/提取整数序列 - Find/extract a sequence of integers within a list in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM