使用字典替换列表中的项目

时间:2012-04-23 01:16:15

标签: python dictionary

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

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

2 个答案:

答案 0 :(得分:3)

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

notes = [allKeys[note] for note in notes]

答案 1 :(得分:1)

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