2
LowKeys = dict(La = 'z', Lb = 'x', Lc = 'c', Ld = 'v', Le = 'b', Lf = 'n', Lg = 'm')
MidKeys = dict(Ma = 'q', Mb = 'w', Mc = 'e', Md = 'r', Me = 't', Mf = 'y', Mg = 'u')
HighKeys = dict(Ha = 'i', Hb = 'o', Hc = 'p', Hd = '[', He = ']')
SharpLowKeys = dict(SLa = 's', SLc = 'f', SLd = 'g', SLf = 'j', SLg = 'k') 
FlatLowKeys = dict(FLa = 'a', FLb = 's', FLd = 'f', FLe = 'g', FLg = 'j') 
SharpMidKeys = dict(SMa = '2', SMc = '4', SMd = '5', SMf = '7', SMg = '8')
FlatMidKeys = dict(FMa = '1', FMb = '2', FMd = '4', FMe = '5', FMg = '7')
SharpHighKeys = dict(SHa = '9', SHc = '-', SHd = '=')
FlatHighKeys = dict(FHa = '8', FHb = '9', FHd = '-', FHe = '=')

notes = raw_input('Notes: ')
notes = notes.split()

我想用值替换出现在两个注释和任何一个中的所有dicts项目dict

前任:

notes = La, Ha, Lb
notes = z, i, x

有没有办法做到这一点?还是比我正在尝试的更好的方法?

4

2 回答 2

3
allKeys = {}
for subdict in (LowKeys, MidKeys, ..., FlatHighKeys):
    allKeys.update(subdict)

notes = [allKeys[note] for note in notes]
于 2012-04-23T01:33:26.493 回答
1

将字典的所有内容放在一个大字典中,然后使用列表推导从字典中提取适当的值。

于 2012-04-23T01:18:40.153 回答