Python 找到重复数列索引中点的最有效算法是什么?

Python 找到重复数列索引中点的最有效算法是什么?,python,c++11,vector,Python,C++11,Vector,我希望能够得到重复点索引的中点 a=[0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1 -1, -1, -1, -1,-1, 0, 0, 0, 0, 0] i、 e.输出_向量[0]是第一个序列的中点的索引0,0,0,0,0 输出向量[1]是第二个重复序列的中点1,1,1,1,1,1,1 输出向量[2]是第二个重复序列的中点-1,-1,-1,-1,-1一种方法是使用itertools.groupby查找组并计算其中点: output_vector = [2, 8, 13,

我希望能够得到重复点索引的中点

a=[0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1 -1, -1, -1, -1,-1, 0, 0, 0, 0, 0]
i、 e.输出_向量[0]是第一个序列的中点的索引
0,0,0,0,0

输出向量[1]是第二个重复序列的中点
1,1,1,1,1,1,1


输出向量[2]是第二个重复序列的中点
-1,-1,-1,-1,-1
一种方法是使用
itertools.groupby
查找组并计算其中点:

output_vector = [2, 8,  13, 19]

一种方法是使用
itertools.groupby
查找组并计算其中点:

output_vector = [2, 8,  13, 19]

itertools
方法可能更好、更干净。尽管如此,这里有一种方法使用
数学
统计
,并通过查找每组数字的开始和结束索引的中位数

from itertools import groupby

a = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1,-1, 0, 0, 0, 0, 0]

groups = [list(g) for _, g in groupby(a)]    
output_vector = [sum(1 for x in groups[:i] for _ in x) + len(x) // 2 for i, x in enumerate(groups)]
# [2, 8, 14, 19]

itertools
方法可能更好、更干净。尽管如此,这里有一种方法使用
数学
统计
,并通过查找每组数字的开始和结束索引的中位数

from itertools import groupby

a = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1,-1, 0, 0, 0, 0, 0]

groups = [list(g) for _, g in groupby(a)]    
output_vector = [sum(1 for x in groups[:i] for _ in x) + len(x) // 2 for i, x in enumerate(groups)]
# [2, 8, 14, 19]

另一个基于itertools的解决方案,但效率更高

import math
import statistics as stat

a = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0]

lastNum = None
startIdx = 0
midpts = []
for idx, x in enumerate(a):
    if lastNum is not None and lastNum != x or idx == len(a) - 1:
        midpts.append(math.floor(stat.median([startIdx, idx])))
        startIdx = idx
    lastNum = x

print(midpts)
# [2, 8, 14, 19]

另一个基于itertools的解决方案,但效率更高

import math
import statistics as stat

a = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0]

lastNum = None
startIdx = 0
midpts = []
for idx, x in enumerate(a):
    if lastNum is not None and lastNum != x or idx == len(a) - 1:
        midpts.append(math.floor(stat.median([startIdx, idx])))
        startIdx = idx
    lastNum = x

print(midpts)
# [2, 8, 14, 19]

基于C++的@Matt M答案的实现

from itertools import groupby

a = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1,-1, 0, 0, 0, 0, 0]

output = []
psum = 0 
for glen in (sum(1 for i in g) for k, g in groupby(a)):
    output.append(psum + glen // 2)
    psum += glen

print(output)

基于C++的@Matt M答案的实现

from itertools import groupby

a = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1,-1, 0, 0, 0, 0, 0]

output = []
psum = 0 
for glen in (sum(1 for i in g) for k, g in groupby(a)):
    output.append(psum + glen // 2)
    psum += glen

print(output)

(1) 找到每个重复值的起点和终点(2)从中计算中点。(1)找到每个重复值的起点和终点(2)从中计算中点。我喜欢这个答案,因为它可以很容易地翻译成其他编程语言!!我喜欢这个答案,因为它可以很容易地翻译成其他编程语言!!