lis中事件间隔的频率

2024-10-01 07:19:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个记录事件发生的列表:

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

1表示在给定的时间步中事件的发生,0表示未观察到事件的时间步。你知道吗

我有兴趣在python上估计事件间隔的统计数据。也就是说,timesI的统计数据没有记录任何事件(0):例如

- mean of interval of events: (2+4+3)/3 = 3
- max duration of an interval of no-event: 4

有什么建议我该怎么做吗?你知道吗

非常感谢


Tags: ofnoan列表记录时间事件events
2条回答
interval_zeros = [len(list(group)) for z,group in itertools.groupby(data) if z== 0]
print sum(interval_zeros)/float(len(interval_zeros))

这是一种做你想做的事的方法。当然有更有效的方法,但这很简单:

a = [0,0,1,1,1,0,0,1,1,0,0,0,0,1,1,1,1,1,0,0,0,1,1]
a = map(str,a) # Convert numbers to strings
a = ''.join(a) # Concatenate all of the strings
a = a.split('1') # Use the handy split function to find the zeros
a = filter(lambda x: len(x) > 0, a) # select the zeros only
a = map(len,a) # convert zero sequences to lengths
print a

这是结果

[2, 2, 4, 3]

相关问题 更多 >