通过分组从python中的列表列表创建新列表

时间:2018-01-11 13:26:57

标签: python

此问题与我的其他问题有关:silence out regions of audio based on a list of time stamps , using sox and python

如果matrix=outer(matext_vec,matext_, FUN=myfun)

新列表q'应为q= [[0.0,4.0], [10.0,12.0], [15.0,20.0], [21.0,28.0], [32.0,36.0],[41.0,44.0]]

我所做的是以下内容:

[4.0,10.0],[12.0,15.0],[20.0,21.0],[28.0,32.0], [36.0,41.0]]

输出:

import numpy
q= [[0.0,4.0], [10.0,12.0], [15.0,20.0], [21.0,28.0], [32.0,36.0],[41.0,44.0]]
x= []       
print "in between"
for t in range(len(q)-1):
    a,b=q[t][1],q[t+1][0]
    x.append([a,b])

for i in x:
    print i

更新:我想在我的^输出中追加两个段。

上下文:这些细分是时间戳。

假设片段没有从零开始,而是从3.0开始 [4.0, 10.0] [12.0, 15.0] [20.0, 21.0] [28.0, 32.0] [36.0, 41.0] 并且文件以50.0结束。

在原始输出中,我想添加区域:q= [[3.0,4.0], [10.0,12.0], [15.0,20.0], [21.0,28.0], [32.0,36.0],[41.0,44.0]][0.0,3]这样我也可以将这些区域排除在外。

为此我只是做了:

[44.0,50.0]

输出:

import numpy
speaker_segments= [[3.0,4.0], [10.0,12.0], [15.0,20.0], [21.0,28.0], [32.0,36.0],[41.0,44.0]]
segments_to_silence = []
starting= 0.0
end= 50.0
# simple output
for t in range(len(speaker_segments)-1):
        a, b = speaker_segments[t][1],speaker_segments[t+1][0]
        segments_to_silence.append([a, b])
val = len(speaker_segments)
y= speaker_segments[val-1][1]


# appending end of segment item and end of file item to output i.e [44.0,50.0]. 
if end >y:
    a,b =y,end
    segments_to_silence.append([a,b]) 

print "appending end regions"
print segments_to_silence

# appending the starting portions  0.0 - 3.0 :
f=speaker_segments[0][0]
if starting < f:
    a=starting
    b=f
    segments_to_silence.append([a,b])
print "appending beginning regions"
print segments_to_silence

是否可以将附加的[0.0,3.0]移动到开头?这样它们就处于一个排序列表中并按照choronological顺序排列?

更新2: 我只需重新排序if条件,使[0.0,x.x]先行,然后是中间,最后是文件末尾[50.0]。

谢谢大家的快速回复! :)

3 个答案:

答案 0 :(得分:5)

使用ziplist comprehension,您可以执行以下操作:

x = [[a[1], b[0]] for a, b in zip(q, q[1:])]

当你使用python 2时,最好使用zip的迭代器版本:itertools.izip

from itertools import izip

x = [[a[1], b[0]] for a, b in izip(q, q[1:])]

编辑:与itertools.islice一样,Jean-François在评论中指出:

from itertools import islice, izip

x = [[a[1], b[0]] for a, b in izip(q, islice(q, 1, None))]

答案 1 :(得分:4)

你可以展平,丢弃第一个然后重新组合:

>>> q = [[0.0,4.0], [10.0,12.0], [15.0,20.0], [21.0,28.0], [32.0,36.0],[41.0,44.0]]
>>> from itertools import chain, islice
>>> list(map(list, zip(*2*(islice(chain(*q), 1, None),))))
[[4.0, 10.0], [12.0, 15.0], [20.0, 21.0], [28.0, 32.0], [36.0, 41.0]]

Python 2版本:

>>> from itertools import chain, islice, izip
>>> map(list, izip(*2*(islice(chain(*q), 1, None),)))

答案 2 :(得分:0)

您还可以使用itertools.groupby

q= [[0.0,4.0], [10.0,12.0], [15.0,20.0], [21.0,28.0], [32.0,36.0],[41.0,44.0]]
new_q = list(itertools.chain.from_iterable(q))
n = [(a, list(b)) for a, b in itertools.groupby(sorted(new_q, key=lambda x:any(a == x for a, b in q)), key=lambda x:any(a == x for a, b in q))]
final_data = [[a, b] for a, b in zip(dict(n)[0], dict(n)[1][1:])]

输出:

[[4.0, 10.0], [12.0, 15.0], [20.0, 21.0], [28.0, 32.0], [36.0, 41.0]]