【问题标题】:single key with multiple values in a dict字典中具有多个值的单个键
【发布时间】:2011-11-08 12:21:25
【问题描述】:

我可以在我的代码中有一个全局字典,如下所示:

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']
        } 

它有一个键和多个值。我需要这个字典,因为我必须确保音素是元音或辅音才能在代码中进一步处理。稍后在代码中我必须执行类似的操作,

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

谢谢。

【问题讨论】:

  • 嗯,实际上你的问题的答案是“是的,你可以。” if phoneme == vovel 的变量是什么?你能提供更多细节吗?

标签: python


【解决方案1】:

使用集合更有效(如果需要,可以将它们分组在 dict 中):

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)

【讨论】:

  • @DrTyrsa 如果我不使用 dict 或 set 并只使用一个列表,那会有效吗?
  • @zingy 集合在查找中比列表更有效。可能它不会成为您的瓶颈,可能会,但为您的任务使用适当的数据结构始终是一个好主意。
【解决方案2】:

是的,你可以这样做,但使用它的代码应该类似于:

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

也就是说,您可能需要考虑使用 set() 而不是列表,以便更快地查找,即

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

【讨论】:

    【解决方案3】:

    您可以创建一个以操作为值的“反向”字典:

    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)
    

    然后直接调用它们:

    action_phoneme[phoneme]()
    

    【讨论】:

    • 这种方式很棒,当然也是我会采用的方式。但是你会想要将音素传递给两个函数 functionVowel 或 functionConsonant 中的任何一个,所以理想情况下你会使用 lambda 或类似的东西绑定值,这样你就不必两次使用相同的参数,即现在你必须打电话说:action_phoneme[phoneme](phoneme)
    • @Benedict - 使用operator.methodcaller 怎么样?查看我的编辑并感谢您的提示。
    • 我喜欢这样。直到现在我才知道methodcaller
    【解决方案4】:

    是的,但您的代码将类似于:

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

    【讨论】:

      【解决方案5】:

      你应该用列表而不是字典来做到这一点。

      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']
      

      所以你可以确定:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-24
        • 1970-01-01
        • 2011-06-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多