[英]create new list from list of lists in python by grouping
This question is related to my other question: silence out regions of audio based on a list of time stamps , using sox and python 这个问题与我的另一个问题有关: 使用sox和python根据时间戳列表使音频区域静音
If 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]]
如果
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 list q' should be [4.0,10.0],[12.0,15.0],[20.0,21.0],[28.0,32.0], [36.0,41.0]]
新列表q'应该为
[4.0,10.0],[12.0,15.0],[20.0,21.0],[28.0,32.0], [36.0,41.0]]
What I did is the following: 我所做的是以下几点:
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
Output: 输出:
[4.0, 10.0]
[12.0, 15.0]
[20.0, 21.0]
[28.0, 32.0]
[36.0, 41.0]
UPDATE : I want to append two more segments to my ^ ouput. 更新 :我想在我的^输出中添加另外两个段。
Context : These segments are time stamps. 上下文 :这些段是时间戳。
Say the segments didn't start at zero and instead started at 3.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]]
and the file ends at say 50.0. 假设细分不是从零开始,而是从3.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]]
,文件结束于50.0。
To my original output , I want to add regions : [0.0,3]
and [44.0,50.0]
So that I can silence out those regions too. 在我的原始输出中,我想添加区域:
[0.0,3]
和[44.0,50.0]
,以便我也可以使这些区域静音。
For this I simply did : 为此,我只是做了:
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
OUTPUT : 输出:
appending end regions:
[[4.0, 10.0], [12.0, 15.0], [20.0, 21.0], [28.0, 32.0], [36.0, 41.0], [44.0, 50.0]]
appending beginning regions:
[[4.0, 10.0], [12.0, 15.0], [20.0, 21.0], [28.0, 32.0], [36.0, 41.0], [44.0, 50.0], [0.0, 3.0]]
Is it possible to move the appended [0.0,3.0] to the beginning ? 是否可以将附加的[0.0,3.0]移到开头? so that they are in a sorted list and in choronological order?
以便它们按排序顺序和时间顺序排列?
UPDATE 2: I just had to reorder the if conditionals so that the [0.0,xx] went first, then the middle and finally the end of file [50.0]. 更新2:我只需要重新排列if条件,以便[0.0,xx]首先出现,然后是文件[50.0]的中间,最后是结尾。
Thank you all for your quick responses! 谢谢大家的快速回复! :)
:)
With zip
and list comprehension you could do the folllowing: 使用
zip
和list理解,您可以执行以下操作:
x = [[a[1], b[0]] for a, b in zip(q, q[1:])]
As you are using python 2 it would be better to use the iterator version of zip
: itertools.izip
当您使用python 2时,最好使用
zip
的迭代器版本: itertools.izip
from itertools import izip
x = [[a[1], b[0]] for a, b in izip(q, q[1:])]
Edit: with itertools.islice
as Jean-François pointed out in the comments: 编辑:与
itertools.islice
一样,让·弗朗索瓦在评论中指出:
from itertools import islice, izip
x = [[a[1], b[0]] for a, b in izip(q, islice(q, 1, None))]
You can flatten, discard the first and then regroup: 您可以展平,丢弃第一个然后重新组合:
>>> 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 version: Python 2版本:
>>> from itertools import chain, islice, izip
>>> map(list, izip(*2*(islice(chain(*q), 1, None),)))
You can also use itertools.groupby
: 您也可以使用
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:])]
Output: 输出:
[[4.0, 10.0], [12.0, 15.0], [20.0, 21.0], [28.0, 32.0], [36.0, 41.0]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.