note_list = ...a list containing all notes in order
bigram_dict = {}
for note in range(1, len(note_list)):
bigram = (note -1, note)
if bigram not in bigram_dict:
bigram_dict[bigram] = 1 / len(note_list)
else:
bigram_dict[bigram] += 1 / len(note_list)
from music21.ext.more_itertools import windowed
from collections import Counter
# assuming s is your Stream
nameTuples = []
for n1, n2 in windowed(s.recurse().notes, 2):
nameTuples.append((n1.name, n2.name))
c = Counter(nameTuples)
totalNotes = len(s.recurse().notes) # py2 cast to float
{k : v / totalNotes for k, v in c.items()}
你可能要找的是一种二元表示法,我通常用字典来处理。这可能有点草率,但你可以很容易地整理一下:
这将为您提供每个二元图的百分比。如果使用Python2.x,则必须使用
bigram_dict[bigram += float(1 / len(note_list))
来避免整数/浮点问题。另外,如果字典给你带来麻烦,你可以尝试使用defaultdict。你知道吗我建议你这样做:
windowed的优点是很容易创建三叉树等
相关问题 更多 >
编程相关推荐