我正在编写一个程序来生成随机12音音阶(音乐理论)并将它们存储在列表中numbers[]
我有一个名为invert()
的函数,它取每个值之间的差值,然后对前一个数字进行操作以反转列表中的间隔。
def invert():
diff = [b - a for (a, b) in zip(numbers[:-1], numbers[1:])]
for a in range(11):
if numbers[a] - diff[a] > 11:
numbers[a+1] = numbers[a] - diff[a] - 12
elif numbers[a] - diff[a] < -11:
numbers[a+1] = numbers[a] - diff[a] + 12
else:
numbers[a+1] = numbers[a] - diff[a]
return numbers
我的问题是如何使用modulo来删除这些if语句的用法。 有没有办法使用mod来包围数字,所以如果差异为-15,它会被重估为-3?
答案 0 :(得分:0)
我使用了问题的这一部分&#34;例如,(1,5,4,7,8,3)倒置的是(1,9,10,7,6,11)&#34;作为指导。
dectave(?...像八度音阶,但是有12个音调而不是8个用于全音阶音阶)可以通过首先去除负值来反转,然后确保我们的值不超出我们的12音符范围。 / p>
def invert():
numbers = [1, 5, 4, 7, 8, 3, 2, 6, 9, 10, 11, 12]
diff = [b - a for (a, b) in zip(numbers[:-1], numbers[1:])]
for a in range(11):
if numbers[a] - diff[a] % -12 < 12:
numbers[a + 1] = numbers[a] - diff[a] % -12
else:
numbers[a + 1] = numbers[a] - diff[a] % 12
if numbers[a] - diff[a] % 12 is 0:
numbers[a + 1] = 12
return numbers
输出 - [1,9,10,7,6,11,12,5,5,4,3,2]