Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python中提取列表中相等的元素序列?_Python_List_Python 2.7_Indexing - Fatal编程技术网

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

如何在Python中提取列表中相等的元素序列?,python,list,python-2.7,indexing,Python,List,Python 2.7,Indexing,我有一个更具体的问题,但我还没有找到答案。我真的很绝望,如果有人知道答案,我会非常高兴。提前感谢您阅读 我在Python中有一个类似以下内容的列表: [h,e,l,l,o,h,e,l,l,o,h,e,l,l,o] 现在我想缩短列表,以便过滤出重复多次的元素块。这意味着此列表变为: [h,e,l,l,o] 有人知道这是怎么回事吗?问题是:列表总是看起来不一样,可能是这样的: [b,y,e,b,y,e,b,y,e] 非常感谢您,我将非常感谢您的回答 这是一种可能的解决方案: def shorten(

我有一个更具体的问题,但我还没有找到答案。我真的很绝望,如果有人知道答案,我会非常高兴。提前感谢您阅读

我在Python中有一个类似以下内容的列表:

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

现在我想缩短列表,以便过滤出重复多次的元素块。这意味着此列表变为:

[h,e,l,l,o]

有人知道这是怎么回事吗?问题是:列表总是看起来不一样,可能是这样的:

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


非常感谢您,我将非常感谢您的回答

这是一种可能的解决方案:

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)
以下是一些例子:

>>> 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']

这是一个可能的解决方案:

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)
以下是一些例子:

>>> 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']

这可以通过一个单行函数处理,如下所示

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))
输出

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

上述解决方案将在运行时传递的列表l视为按索引顺序排列的字符序列

它利用regex模式^[a-z]+\1+$来确定整个str是否由重复的子字符串组成,即l从头到尾是否由单个重复模式组成

如果此模式在l str上产生匹配,则返回表示此重复模式匹配组1\1的列表


如果没有匹配-即l不完全由单个重复模式组成-则返回一个与运行时传递的l相同的列表。

这可以通过一个单行函数很好地处理,见下文

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))
输出

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

上述解决方案将在运行时传递的列表l视为按索引顺序排列的字符序列

它利用regex模式^[a-z]+\1+$来确定整个str是否由重复的子字符串组成,即l从头到尾是否由单个重复模式组成

如果此模式在l str上产生匹配,则返回表示此重复模式匹配组1\1的列表


如果未进行匹配(即l不完全由单个重复模式组成),则返回与运行时传递的l相同的列表。

第一个条目是否始终是重复模式的一部分?是否使用大写和小写?如果你也需要的话,我可以整合一下…第一个条目总是重复模式的一部分吗?你用大写和小写吗?如果你也需要的话,我可以整合一下……非常感谢你!:-抱歉,这不会通过所有场景ls=[m,o,m,m,y,m,o,m,m,y];shortenls@ParvathirajanNatarajan谢谢你指出,我修正了错误,非常感谢你-抱歉,这不会通过所有场景ls=[m,o,m,m,y,m,o,m,m,y];shortenls@ParvathirajanNatarajan谢谢你指出,我改正了错误