asqapro
asqapro

Reputation: 175

Replace items in a list using a 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()

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?

Upvotes: 2

Views: 2992

Answers (2)

John La Rooy
John La Rooy

Reputation: 304175

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

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

Upvotes: 3

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798676

Put all the contents of the dicts in one big dict, then use a list comprehension to pull the appropriate values from the dict.

Upvotes: 1

Related Questions