我正在构建一个音乐教练游戏,其中每一个在12节课之后,C,CD,D,DE,E,F,FG,G,GA,A,AB,B是0到11的代表,和弦是这些数字的列表,所以CEG是0,4,7。
看看这个差异,我们得到的间隔是: 4-0=4,7-4=3,SO4,3,在这个例子中,小的三分之一堆放在主要的三分之一之上。
假设我有一个完整的k音符和弦的列表,可能是通过这样做形成的:
pitchClasses = [i for i in range(12)]
from itertools import combinations
deck = list( combinations(pitchClasses, k) )我如何修剪我的甲板,以拒绝任何包含 q间隔的和弦?
例如,我设置了p=3,q=4,然后C,D,G被排除,因为它是0,2,7,带间隙2,5和2<3。还有5>4,所以它被排除在两种情况下。
另外,B,C也将被排除在外,因为11,0 ->缺口为1。
CEG不会被排除,它有间隙4,3。G和C之间也有7的间隙,但这并不构成拒绝。
如果一个3音符和弦产生一个3间隙的“双”音符,它会更干净。因为总差距可能不是最大的差距。
然后,最小元可与p相比较,第二大元可与q比较。
我认为我需要创建“双重”差距--列表,首先排序最小,然后进行比较:
if W[0] < p or W[-2] > q:
# reject我在C#中做了这件事,它产生了相当多的代码。
在Python中有任何方法可以简洁地做到这一点吗?
到目前为止我发现:
How can I find all the subsets of a set, with exactly n elements?
Python - Differences between elements of a list
到目前为止,我的代码是这样的:
from random import shuffle
from itertools import combinations
pitchClasses = [i for i in range(12)]
deck = list( combinations(pitchClasses, notesInChord) )
def GapOk(chord,min_gap,max_gap):
gaps = [ ( 12 + chord[i+1]-chord[i] ) % 12 for i in range(len(chord)) ]
intervals = sorted( gaps )
return intervals[0] >= min_gap and intervals[-2] <= max_gap
deck_pruned = [ chord for chord in deck if GapOk(chord,min_gap,max_gap) ]
shuffle( deck_pruned )看来我已经回答了我自己的问题。但我还是要把它挂起来,因为我很感兴趣是否有更好的方法。稍后我可能会把上面的代码块分解成一个答案。
发布于 2014-06-01 23:06:29
我将让代码检查一遍:
# prefer to use full paths for people less familiar
# from random import shuffle
# from itertools import combinations # with your libs
import random
import itertools
# pitchClasses = [i for i in range(12)] # just materialize into list:
# _ preferred to camelcase for non-native-English readers
pitch_classes = list(range(12))
# no need for extra spaces inside container chars, format like English
# deck = list(itertools.combinations(pitchClasses, notesInChord))
# maybe 3, 4, & 5 note chords?
chord_numbers = (3, 4, 5)
deck = []
for notes_in_chord in chord_numbers:
deck.extend(itertools.combinations(pitch_classes, notes_in_chord))
def GapOk(chord, min_gap, max_gap):
# not sure what you're doing here, so I'll leave off for now, revisit later
# for now, suggest docstring with explanation
gaps = [(12 + chord[i+1]-chord[i]) % 12 for i in range(len(chord))]
intervals = sorted(gaps)
return intervals[0] >= min_gap and intervals[-2] <= max_gap
deck_pruned = [chord for chord in deck if GapOk(chord, min_gap, max_gap)]
random.shuffle(deck_pruned)https://stackoverflow.com/questions/23985328
复制相似问题