2017-08-11 103 views
1

我有'1和'0'的列表,我想計算連續的'1'的組數。連續1的計數組

mylist = [0,0,1,1,0,1,1,1,1,0,1,0] 

這樣做給我們3組,但有沒有辦法做到這一點蟒蛇?

+0

這看起來像一個'groupBy'功能的工作,雖然我不知道是否有Python的這樣的功能標準。搜索該名稱的文檔。 – Carcigenicate

+0

在python中有一個groupby函數---儘管它主要用於將行分組在一起 – user44840

+0

是的。對不起,這很新鮮。你的答案是最簡潔的。也不太複雜。 – user44840

回答

1

選項1

隨着pandas。首先,初始化一個數據幀:

In [78]: df 
Out[78]: 
    Col1 
0  0 
1  0 
2  1 
3  1 
4  0 
5  1 
6  1 
7  1 
8  1 
9  0 
10  1 
11  0 

現在,通過團數的計算總和:

In [79]: df.sum()/df.diff().eq(1).cumsum().max() 
Out[79]: 
Col1 2.333333 
dtype: float64 

如果你想羣體的只是數量,df.diff().eq(1).cumsum().max()就夠了。


選項2

隨着itertools.groupby

In [88]: sum(array)/sum(1 if sum(g) else 0 for _, g in itertools.groupby(array)) 
Out[88]: 2.3333333333333335 

如果你想羣體的只是數量,sum(1 if sum(g) else 0 for _, g in itertools.groupby(array))就夠了。

1

你可以試試這個

import numpy as np 
import pandas as pd 
df=pd.DataFrame(data = [0,0,1,1,0,1,1,1,1,0,1,0]) 
df['Gid']=df[0].diff().eq(1).cumsum() 
df=df[df[0].eq(1)] 
df.groupby('Gid').size() 
Out[245]: 
Gid 
1 2 
2 4 
3 1 
dtype: int64 

sum(df.groupby('Gid').size())/len(df.groupby('Gid').size()) 
Out[244]: 2.3333333333333335 
1

這裏有一個解決方案:

durations = [] 

for n, d in enumerate(mylist): 
    if (n == 0 and d == 1) or (n > 0 and mylist[n-1] == 0 and d == 1): 
     durations.append(1) 
    elif d == 1: 
     durations[-1] += 1 

def mean(x): 
    return sum(x)/len(x) 

print(durations) 
print(mean(durations)) 
1

你可以試試這個:

mylist = [0,0,1,1,0,1,1,1,1,0,1,0] 
previous = mylist[0] 
count = 0 

for i in mylist[1:]: 
    if i == 1: 
     if previous == 0: 
      previous = 1 
    else: 
     if i == 0: 
      if previous == 1: 
       count += 1 
       previous = 0 

print count 

輸出:

3 
2

在這裏,我算每當有從0到1 跳在前面加上0防止不算領先的序列。

import numpy as np 

mylist_arr = np.array([0] + [0,0,1,1,0,1,1,1,1,0,1,0]) 
diff = np.diff(mylist_arr) 
count = np.sum(diff == 1) 
1

itertools.groupby看看:

import itertools 
import operator 

def get_1_groups(ls): 
    return sum(map(operator.itemgetter(0), itertools.groupby(ls))) 

這工作,因爲itertools.groupby收益(可迭代當量):

itertools.groupby([0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0]) 
# ==> 
[(0, [0, 0]), (1, [1, 1]), (0, [0]), (1, [1, 1, 1, 1]), (0, [0]), (1, [1]), (0, [0])] 

所以你只是總結的第一個項目。

如果您可以有其他不是0的項目,他們會增加總和。

你可以做這樣的事情:

def count_groups(ls, target=1): 
    return sum(target == value for value, _ in itertools.groupby(ls)) 
+0

如何將get_group()應用於數據框? – user44840

1

這可以無需太多工作就簡單地相加的次數列表轉換從01(計數rising signal edges)來完成:

count = 0 
last = 0 
for element in mylist: 
    if element != last: 
     last = element 
     if element: # 1 is truthy 
      count += 1 
print count 
-1

快速骯髒的單線(幾乎)

import re 
mylist = [0,0,1,1,0,1,1,1,1,0,1,0] 
print len(re.sub(r'0+', '0', ''.join(str(x) for x in mylist)).strip('0').split('0')) 
3 

一步一步:

import re 
mylist = [0,0,1,1,0,1,1,1,1,0,1,0] 
sal1 = ''.join(str(x) for x in mylist) # returns a string from the list 
sal2 = re.sub(r'0+', '0', sal1) # remove duplicates of zeroes 
sal3 = sal2.strip('0')   # remove 0s from the start & the end of the string 
sal4 = len(sal3.split('0'))  # split the string using '0' as separators into a list, and calculate it's length 

此拋出:

sal -> 001101111010 
sal2 -> 01101111010 
sal3 -> 110111101 
sal4 -> 3 
+0

嘿,爲什麼downvote? –