python-如何计算python数组中“不间断”重复的数目?

时间:2019-01-30 18:07:20

标签: python numpy

我有一个0.1的numpy数组,像这样:

 [0,0,0,1,1,1,0,0,1,1,0,0,0,1,1,1,1,0,0,0]

我想拥有一个告诉我在该数组中分别将数字1重复3,2,4次的函数。为此有一个简单的numpy函数吗?

3 个答案:

答案 0 :(得分:2)

这是这样做的一种方法,它首先找到簇,然后使用Counter获得簇的频率。第一部分从this二维数组的答案中得到启发。我添加了第二个Counter部分以获得所需的答案。

如果发现链接的原始答案有帮助,请访问并对其进行投票。

from scipy.ndimage import measurements
from collections import Counter

arr = np.array([0,0,0,1,1,1,0,0,1,1,0,0,0,1,1,1,1,0,0,0])

cluster, freq = measurements.label(arr)

print (list(Counter(cluster).values())[1:])
# [3, 2, 4]

答案 1 :(得分:1)

假设您只有0和1:

import numpy as np
a = np.array([0,0,0,1,1,1,0,0,1,1,0,0,0,1,1,1,1,0,0,0])

# pad a with 0 at both sides for edge cases when a starts or ends with 1
d = np.diff(np.pad(a, pad_width=1, mode='constant'))
# subtract indices when value changes from 0 to 1 from indices where value changes from 1 to 0
np.flatnonzero(d == -1) - np.flatnonzero(d == 1)
# array([3, 2, 4])

答案 2 :(得分:0)

自定义实现?

def count_consecutives(predicate, iterable):
  tmp = []
  for e in iterable:
    if predicate(e): tmp.append(e)
    else:
      if len(tmp) > 0: yield(len(tmp)) # > 1 if you want at least two consecutive
      tmp = []
  if len(tmp) > 0: yield(len(tmp)) # > 1 if you want at least two consecutive

因此您可以:

array = [0,0,0,1,1,1,0,0,1,1,0,0,0,1,1,1,1,0,0,0]
(count_consecutives(lambda x: x == 0, array)
#=> [3, 2, 4]

还有:

array = [0,0,0,1,2,3,0,0,3,2,1,0,0,1,11,10,10,0,0,100]
count_consecutives(lambda x: x > 1, array)
# => [2, 2, 3, 1]