【问题标题】:create new list from list of lists in python by grouping通过分组从python中的列表列表创建新列表
【发布时间】:2018-01-11 13:26:57
【问题描述】:

这个问题和我的另一个问题有关:silence out regions of audio based on a list of time stamps , using sox and python

如果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' 应该是 [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

输出:

[4.0, 10.0]
[12.0, 15.0]
[20.0, 21.0]
[28.0, 32.0]
[36.0, 41.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 结尾。

在我的原始输出中,我想添加区域:[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

输出:

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]]   

是否可以将附加的 [0.0,3.0] 移动到开头?以便它们在排序列表中并按时间顺序排列?

更新 2: 我只需要重新排序 if 条件,以便 [0.0,x.x] 首先出现,然后是中间,最后是文件 [50.0] 的结尾。

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

【问题讨论】:

  • 如何更好?如题,效率更高?还是更简洁的代码?
  • 抱歉没有澄清。如果有更有效的方法来做到这一点。我将这些最终输出传递给填充和修剪函数,以消除我传递的任何内容。

标签: python


【解决方案1】:

使用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 在 cmets 中指出的那样:

from itertools import islice, izip

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

【讨论】:

  • slice 的迭代器版本q[1:],避免所有临时列表创建。
【解决方案2】:

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

>>> 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),)))

【讨论】:

  • 这只是更少的行但不是更少的代码,而且绝对不是更容易理解......
【解决方案3】:

你也可以使用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]]

【讨论】:

    猜你喜欢
    • 2022-08-13
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-10
    相关资源
    最近更新 更多