python - Python find duplicate adjacent sequences in list
问题描述
I have a list of positive integer numbers (indices) and want to find cycles (here: duplicate adjacent sequences of list elements) as part of an iterative algorithm. The number of iterations is less than 10^6. In every iteration, one number is appended to the list (initially the list is empty) and the list should be checked for cycles. The list size should typically be less than 10^6, usually in the range 0 - 10^5, and the integers will likely be smaller than 10^6.
Examples for lists with cycles (here, more generally, the lists may extend beyond cycles and may contain multiple, even nested, cycles):
- [0,5,6,1,3,5,6,1,3,8,7,0] --> [5,6,1,3]
- [0,2,4,3,3,3,0,1,6,7] --> [3],[3]
- [2,1,1,3,3,1,1,3,3,4,5] --> [1],[3],[1],[3],[1,1,3,3]
- [7,6,2,3,2,1,7,1,9,1,7,1,9,0,4] --> [1,7,1,9]
Examples for lists with no cycles:
- [0,5,6,3,5,6,1,3,8,7,0]
- [0,2,4,0,1,6,7]
- [2,1,3,4,5]
- [7,6,2,3,2,1,7,1,9,1,7,1,0,4]
What is the fastest (and optionally most elegant/pythonic) way to**
- find if there are cycles, return True or False, and
- optionally: return all cycles, with or without any kind of cycle ordering (e.g. by index of first cycle element in list, then by cycle length)?
解决方案
这是我能给的最好的。
from math import floor
sample = samples[0]
def find_max_seq(sample):
max_possible_size_ans = floor(len(sample)/2)
possible_size_ans = list(range(1,max_possible_size_ans+1))
max_idx = len(sample)-1
outs = []
for size in possible_size_ans:
last_idx= 0
start_idx = 0
while last_idx < max_idx :
first_pair = sample[start_idx:start_idx+size]
second_start = start_idx+size
second_pair = sample[second_start:second_start+size]
if first_pair == second_pair:
outs.append(first_pair)
start_idx +=1
last_idx = second_start+size
return outs
推荐阅读
- django - 如何从弹出模式引导程序中的链接运行函数
- c# - 很难理解级联删除的工作原理以及如何配置关系
- c# - 如何知道一个小时的文件夹中是否没有新文件
- batch-file - 尝试批量制作“操作系统”
- visual-studio - 在 Visual Studio 2019 中调试时 Dockerfile 似乎没有运行
- reactjs - 如何在没有 redux saga 的情况下调用 reducer 操作
- python-3.x - 无法使用 python 编辑 Heroku 上托管的文件
- powershell - 将经典 SharePoint 页面转换为新式时出错 - 索引超出范围
- python - 如何动态使用不影响整个环境的产量
- typo3 - 如何使用翻译服务器更新 TYPO3 默认语言?