使用词典替换列表中的项

2024-10-01 00:22:18 发布

您现在位置:Python中文网/ 问答频道 /正文

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()

我想用dict值替换出现在注释和dicts中的所有项。在

例如:

^{pr2}$

有办法吗?或者是比我所尝试的更好的方法?在


Tags: lembmcmddictlanoteslc
2条回答
allKeys = {}
for subdict in (LowKeys, MidKeys, ..., FlatHighKeys):
    allKeys.update(subdict)

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

把所有的内容放在一个大的dict中,然后使用列表理解从dict中提取适当的值

相关问题 更多 >