这个问题与我的另一个问题有关: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]的中间,最后是结尾。
谢谢大家的快速回复! 🙂
参考方案
使用zip和list 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))]
Python sqlite3数据库已锁定 - python
我在Windows上使用Python 3和sqlite3。我正在开发一个使用数据库存储联系人的小型应用程序。我注意到,如果应用程序被强制关闭(通过错误或通过任务管理器结束),则会收到sqlite3错误(sqlite3.OperationalError:数据库已锁定)。我想这是因为在应用程序关闭之前,我没有正确关闭数据库连接。我已经试过了: connectio…Python-Excel导出 - python
我有以下代码:import pandas as pd import requests from bs4 import BeautifulSoup res = requests.get("https://www.bankier.pl/gielda/notowania/akcje") soup = BeautifulSoup(res.cont…Python:传递记录器是个好主意吗? - python
我的Web服务器的API日志如下:started started succeeded failed 那是同时收到的两个请求。很难说哪一个成功或失败。为了彼此分离请求,我为每个请求创建了一个随机数,并将其用作记录器的名称logger = logging.getLogger(random_number) 日志变成[111] started [222] start…Python pytz时区函数返回的时区为9分钟 - python
由于某些原因,我无法从以下代码中找出原因:>>> from pytz import timezone >>> timezone('America/Chicago') 我得到:
我的正则表达式:vendor = "MyNameIsJoe. I'mWorkerInAAAinc." ven = re.split(r'(?<=[a-z])[A-Z]|[A-Z](?=[a-z])', vendor) 以大写字母分割字符串,例如:'我的名字是乔。 I'mWorkerInAAAinc”变成…