Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.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 在列表中查找大小为n或更小的最大重复_Python_Python 3.x - Fatal编程技术网

Python 在列表中查找大小为n或更小的最大重复

Python 在列表中查找大小为n或更小的最大重复,python,python-3.x,Python,Python 3.x,我有这样一个元素列表: ['x', 'a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'g', 'h', 'i', 'i', 'i', 'i'] 我想找出n元素及其以下的所有“最大”重复,以及每个序列重复的次数。例如,如果n=3: >>> [(['a', 'b', 'c'], 3), (['g', 'h'], 2), (['i'], 4)] 我也不想返回(['I','I'],2),因为

我有这样一个元素列表:

['x', 'a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'g', 'h', 'i', 'i', 'i', 'i']
我想找出
n
元素及其以下的所有“最大”重复,以及每个序列重复的次数。例如,如果
n=3

>>> [(['a', 'b', 'c'], 3), (['g', 'h'], 2), (['i'], 4)]
我也不想返回
(['I','I'],2)
,因为有一个较长的序列涉及元素
'I'

这是第二个条件:

['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'g', 'h', 'i', 'i', 'i', 'i']
>>> [(['a', 'b', 'c'], 3), (['b', 'c'], 2), (['g', 'h'], 2), (['i'], 4)]
可接受属于2个不同重复的元素重叠

我在考虑一个基于滑动窗口的解决方案,滑动窗口的大小
n
,并逐渐减小,跟踪已经使用的索引,但我没有满足第一个条件

有没有一种有效的方法可以做到这一点?

您可以使用正则表达式:

>>> li=['x', 'a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'g', 'h', 'i', 'i', 'i', 'i']
>>> [(t[0],''.join(t).count(t[0])) for t in re.findall(r'(\w+)(\1+)', ''.join(li))]
[('abc', 3), ('gh', 2), ('ii', 2)]
或者


您可以创建一个函数:

import re
def counting(x):
    d = re.sub(r"(?<=(\w))(?=\1)","\n","\n".join(re.findall(r"(\w+)(?=\1)",''.join(x)))).split()
    return  [(list(i),d.count(i)+1)for i in set(d)]

重复必须连续出现吗?我认为你的逻辑有一个缺陷——长度为1的序列总是等于或长于包含它们的较大序列。如果你有
3['a'、'b'、'c']
,你保证至少有
3['a']
,它们必须是连续的。这更有意义,它不处理我提到的第一个条件,但我觉得我可以添加一个函数,过滤掉('xxx')元素并处理
(['x'],n)
后角大小写。您将如何修改解决方案以处理大小大于1的元素,如
“xyz”
?由于列表不加区分地连接到单个字符串中,多字符元素将成为整个字符串的一部分。因此,如果您将
'b'
完全替换为
'xyz'
,您将得到
[('axyzc',3),('gh',2),('ii',2)]
的结果。是的,我知道,这就是为什么我想知道如何修改您的解决方案以解开多字符元素。在这种情况下,“句柄”是什么意思?你需要明确输入和期望的输出。这不符合预期,因为它会计算非连续的重复。使用
seq='aabbaa'
,它返回
[(['a'],3),(['b'],2)]
而不是
[(['a'],2),(['b'],2)]
是的,注释中明确指出重复必须是连续的。没有连续的3
a
序列,只有2个序列。问题是每个序列必须有一个唯一的键。。例如,在示例中,我们有abc、bc等,我们正在计算这些。现在在你的情况下,你又有了
aa
然后
bb
然后
aa
!!所以这不是唯一的钥匙!!!它将被添加到前面的
aa
-1@ThierryLathuille让我来帮助您,您希望从
aaaabbbaabbabb
中得到什么样的答案????
import re
def counting(x):
    d = re.sub(r"(?<=(\w))(?=\1)","\n","\n".join(re.findall(r"(\w+)(?=\1)",''.join(x)))).split()
    return  [(list(i),d.count(i)+1)for i in set(d)]
m = ['x', 'a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'g', 'h', 'i', 'i', 'i', 'i']

counting(m)
[(['g', 'h'], 2), (['i'], 4), (['a', 'b', 'c'], 3)]


n = ['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'g', 'h', 'i', 'i', 'i', 'i']

counting(n)
[(['g', 'h'], 2), (['i'], 4), (['a', 'b', 'c'], 3), (['b', 'c'], 2)]