我试图通过使用if语句,for循环和列表来运行它。该列表是参数的一部分。我不知道如何编写if语句并让程序循环遍历所有不同的单词并设置它应该是如何。
newSndIdx=0;
for i in range (8700, 12600+1):
sampleValue=getSampleValueAt(sound, i)
setSampleValueAt(newSnd, newSndIdx, sampleValue)
newSndIdx +=1
newSndIdx=newSndIdx+500
for i in range (15700, 17600+1):
sampleValue=getSampleValueAt(sound, i)
setSampleValueAt(newSnd, newSndIdx, sampleValue)
newSndIdx +=1
newSndIdx=newSndIdx+500
for i in range (18750, 22350+1):
sampleValue=getSampleValueAt(sound, i)
setSampleValueAt(newSnd, newSndIdx, sampleValue)
newSndIdx +=1
newSndIdx=newSndIdx+500
for i in range (23700, 27250+1):
sampleValue=getSampleValueAt(sound, i)
setSampleValueAt(newSnd, newSndIdx, sampleValue)
newSndIdx +=1
newSndIdx=newSndIdx+500
for i in range (106950, 115300+1):
sampleValue=getSampleValueAt(sound, i)
setSampleValueAt(newSnd, newSndIdx, sampleValue)
newSndIdx+=1
答案 0 :(得分:2)
怎么样(如果需要的话):
ranges = (
(8700, 12600),
(15700, 17600),
(18750, 22350),
(23700, 27250),
(106950, 115300),
)
newSndIdx = 0
for start, end in ranges:
for i in range(start, end + 1):
sampleValue = getSampleValueAt(sound, i)
setSampleValueAt(newSnd, newSndIdx, sampleValue)
newSndIdx += 1
newSndIdx += 500
答案 1 :(得分:0)
我想我知道你在这里寻找什么。如果是这样,它很笨拙; GaretJax重新设计的方式更简单,更清晰(引导效率更高一些)。但它是可行的:
# Put the ranges in a list:
ranges = [
(8700, 12600),
(15700, 17600),
(18750, 22350),
(23700, 27250),
(106950, 115300),
]
newSndIdx = 0
# Write a single for loop over the whole range:
for i in range(number_of_samples):
# If the song is in any of the ranges:
if any(r[0] <= i <= r[1] for r in ranges):
# Do the work that's the same for each range:
sampleValue=getSampleValueAt(sound, i)
setSampleValueAt(newSnd, newSndIdx, sampleValue)
newSndIdx +=1
然而,这仍然缺少你为每个范围添加500的位;要做到这一点,你需要另一个if
,如:
if any(r[0] <= i <= r[1] for r in ranges):
if any(r[0] == i for r in ranges[1:]):
newSndIdx += 500
# The other stuff above