小编Tim*_*Tim的帖子

如何从具有元组集和列表的现有字典创建嵌套字典

我已经解析了一个midi文件,并且我已经成功地得到了一个按乐器分解的音符字典。note_dict下面是一个简短的例子,为了这个问题的目的而被截断。

我的最终目标是拥有一个嵌套字典,为我提供曲目名称,然后将每个可能的音符作为键,然后将所有可能的“下一个”音符列表作为值。目的是将其用作Foxdot 中马尔可夫链,这是一个用于音乐生成的 Python 接口。

它应该看起来像:

{'track1': {note: [note1, note2, note3], note2: [note1, note2, note3]}, 'track2': {note: [note1, note2, note3], note2: [note1, note2, note3]}
Run Code Online (Sandbox Code Playgroud)

这是我所拥有的一个例子:

import itertools 

def pairwise(iterable):
    a, b = itertools.tee(iterable)
    next(b, None)
    return list(zip(a, b))

note_dict = {'Vocal': [-2, -2, -1, -2], 'Guitar': [1, 1, 4, 1, -2, 1]}

note_dict_updated = { track: [{ n for n in notes }, pairwise(notes), notes] for track, notes in note_dict.items() }
print(note_dict_updated)
Run Code Online (Sandbox Code Playgroud)

这给了我以下内容,其中第一组是所有不同的音符,元组列表是 的配对 …

python midi dictionary markov-chains mido

8
推荐指数
1
解决办法
144
查看次数

标签 统计

dictionary ×1

markov-chains ×1

midi ×1

mido ×1

python ×1