zingy
zingy

Reputation: 811

single key with multiple values in a dict

can I have a global dict in my code which is something like the following:

group = { 
        'vowel' : ['aa', 'ae', 'ah', 'ao', 'eh', 'er', 'ey', 'ih', 'iy', 'uh', 'uw', 'o'],
        'consonant' : ['b', 'ch', 'd', 'dh', 'dx', 'f', 'g', 'hh', 'jh', 'k', 'l', 'm', 'n', 'ng', 'p', 'r', 's', 'sh', 't', 'th', 'v', 'w', 'y', 'z', 'zh']
        } 

It has a single key and multiple values. I need this dict because I have to make sure a phoneme is either a vowel or consonant to proceed further in the code. Later in the code I have to do something like,

if phoneme == vowel :
    do this
else :
    do that (for consonants)     

Thank you.

Upvotes: 0

Views: 458

Answers (5)

eumiro
eumiro

Reputation: 212835

You can create a "reverse" dictionary with the action as value:

import operator as op

group = { 
        'vowel' : ['aa', 'ae', 'ah', 'ao', 'eh', 'er', 'ey', 'ih', 'iy', 'uh', 'uw', 'o'],
        'consonant' : ['b', 'ch', 'd', 'dh', 'dx', 'f', 'g', 'hh', 'jh', 'k', 'l', 'm', 'n', 'ng', 'p', 'r', 's', 'sh', 't', 'th', 'v', 'w', 'y', 'z', 'zh']
        } 

# define methods functionVowel and functionConsonant

action = {'vowel': lambda x: op.methodcaller(functionVowel, x),
          'consonant': lambda x: op.methodcaller(functionConsonant, x)}

action_phoneme = dict((char, action[k]) for k,v in group.iteritems() for phoneme in v)

and then call them directly:

action_phoneme[phoneme]()

Upvotes: 4

jonathan.hepp
jonathan.hepp

Reputation: 1643

You should do this with lists instead of dicts.

vowel = ['aa', 'ae', 'ah', 'ao', 'eh', 'er', 'ey', 'ih', 'iy', 'uh', 'uw', 'o']
consonant = ['b', 'ch', 'd', 'dh', 'dx', 'f', 'g', 'hh', 'jh', 'k', 'l', 'm', 'n', 'ng', 'p', 'r', 's', 'sh', 't', 'th', 'v', 'w', 'y', 'z', 'zh']

So you can determinate:

if phoneme is in vowel:
    do this
else:
    do that

Upvotes: 1

Shawn Chin
Shawn Chin

Reputation: 86854

Yes, you can do that, but the code to use it should probably look something like:

if phoneme in group["vowel"]:
    # ...

That said, you might want to consider using set() instead of a list to give you faster lookups, i.e.

group = { 
        'vowel' : set(('aa', 'ae', 'ah', 'ao', 'eh', 'er', 'ey', 'ih', 'iy', 'uh', 'uw', 'o')),
        'consonant' : set(('b', 'ch', 'd', 'dh', 'dx', 'f', 'g', 'hh', 'jh', 'k', 'l', 'm', 'n', 'ng', 'p', 'r', 's', 'sh', 't', 'th', 'v', 'w', 'y', 'z', 'zh')),
} 

Upvotes: 4

DrTyrsa
DrTyrsa

Reputation: 31951

It's more effective to use sets (you can group them in dict if you want):

vowels = set(['aa', 'ae', 'ah', 'ao', 'eh', 'er', 'ey', 'ih', 'iy', 'uh', 'uw', 'o'])
consonants = set(['b', 'ch', 'd', 'dh', 'dx', 'f', 'g', 'hh', 'jh', 'k', 'l', 'm', 'n', 'ng', 'p', 'r', 's', 'sh', 't', 'th', 'v', 'w', 'y', 'z', 'zh'])


if phoneme in vowels:
    do this
else :
    do that (for consonants)

Upvotes: 12

Fábio Diniz
Fábio Diniz

Reputation: 10353

Yes, but your code will be something like:

if phoneme in group['vowel'] :
    do this
else :
    do that (for consonants)     

Upvotes: 2

Related Questions