使用字典替换列表中的项目 [英] Replace items in a list using a dictionary

查看:106
本文介绍了使用字典替换列表中的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

LowCeys = 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' $ b SharpHighKeys = dict(SHa ='9',SHc =' - ',SHd ='=')
FlatHighKeys = dict(FHa ='8',FHb ='9',FHd =' - FHe ='=')

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

t用 dict 值替换在两个注释和任何 dict 中出现的所有项目。



例如:

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

有没有办法这样做?或者比我想要的更好的方法?

解决方案

  allKeys = {} 
用于在(LowKeys,MidKeys,...,FlatHighKeys)中的子进程:
allKeys.update(subtict)

notes = [allKeys [note]注释中的注释]


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

I want to replace all the items that appear in both notes and any of the dicts with the dict value.

Ex:

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

Is there a way to do this? Or a better way than what I'm trying?

解决方案

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

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

这篇关于使用字典替换列表中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆