所以我有一个包含两个数字的列表,分别是1和0。列表中的每个项目都相当于1秒钟的音频。 0是语音,而1是音乐。
列表示例:
[1,1,1,1,1,1,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1,1,1,1]
我希望输出显示音频的开始,音频的结束和音频的类型。
输出:
1,6, music
7,12, speech
13,17, music
18,24, speech
25,26, music
27,32, speech
33,37, music
答案 0 :(得分:0)
a = [1,1,1,1,1,1,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1,1,1,1]
changes = [i+1 for i,val in enumerate(a[:-1]) if val != a[i+1]]
lists_start_end = [(1,changes[0])] + [(val+1,changes[i+1]) for i,val in enumerate(changes[:-1])] + [(changes[-1],len(a))]
出局:
[(1, 6), (7, 12), (13, 17), (18, 24), (25, 26), (27, 32), (32, 37)]
答案 1 :(得分:0)
In[2]: data = [1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1]
In[3]: from itertools import groupby
...:
...: dex = 1
...: result = []
...: for k, g in groupby(data):
...: length = sum(1 for _ in g)
...: result.append((
...: (dex, dex + length - 1),
...: 'music' if k == 1 else 'speech'
...: ))
...: dex += length
...:
In[4]: result
Out[4]:
[((1, 6), 'music'),
((7, 12), 'speech'),
((13, 17), 'music'),
((18, 24), 'speech'),
((25, 26), 'music'),
((27, 32), 'speech'),
((33, 37), 'music')]