clave única con varios valores en un dictado

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)     

Gracias por su atención.

preguntado el 08 de noviembre de 11 a las 12:11

Well, actually the answer to your question is "Yes, you can." What are the variables the if phoneme == vovel? Can you give more details? -

5 Respuestas

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)

respondido 08 nov., 11:16

@DrTyrsa if I don't use dict or set and use just a list then will that be efficient? - zingy

@zingy Sets are more efficient in lookups than lists. May be it won't be your bottleneck, may be it will, but it's always a good idea to use proper data structures for your task. - DrTyrsa

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')),
} 

respondido 08 nov., 11:16

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

respondido 08 nov., 11:17

This way is great and certainly the approach I would take. But uou're going to want to pass the phoneme into either of the two functions functionVowel or functionConsonant, so ideally you'd bind the value using a lambda or somesuch so that you don't have to use the same argument twice i.e. presently you have to call say: action_phoneme[phoneme](phoneme) - Benedicto

@Benedict - how about using operator.methodcaller? See my edit and thanks for the tip. - eumiro

I like that. I wasn't aware of methodcaller hasta ahora. - Shawn Chin

Yes, but your code will be something like:

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

respondido 08 nov., 11:16

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

respondido 08 nov., 11:16

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.