【问题标题】:python - how to compare a key of the dictionary with a string characterpython - 如何将字典的键与字符串字符进行比较
【发布时间】:2015-02-23 07:20:34
【问题描述】:

我是 python 新手,我需要将字符串中的字符与字典中的键进行比较。但是我无法想出一种方法来将该字符与键进行比较。我只能将它与 dict[key] 的值进行比较

我正在尝试实现这样的东西:

score = {"a": 1, "c": 3, "b": 3, "e": 1, "d": 2, "g": 2, 
     "f": 4, "i": 1, "h": 4, "k": 5, "j": 8, "m": 3, 
     "l": 1, "o": 1, "n": 1, "q": 10, "p": 3, "s": 1, 
     "r": 1, "u": 1, "t": 1, "w": 4, "v": 4, "y": 4, 
     "x": 8, "z": 10}

def compare(word):
   res = 0
   for letter in word:
       if score[**What should i put in here**] == letter:
          res += score[letter]
   return res

其中 score[key] 表示该特定键的值作为一个整体。有没有办法将键与字母进行比较,而不是值?

我的目标是将“word”中的“字母”与字典中的键进行比较,并将值与字符相加并返回结果。

【问题讨论】:

  • for letter in word: res += score.get(letter, 0)
  • 您是否要检查该字母是否作为键属于字典?
  • dict 应该是score...吗?

标签: python dictionary


【解决方案1】:

看起来你在奇怪地思考这个问题。您需要做的就是检查该字母是否在您的 score 字典中,如果是,则将该数字添加到您的总数中。

def compare(word):
    res = 0
    for letter in word:
        if letter in score:
            res += score[letter]
    return res

但是有一种更简单的方法可以做到这一点。由于您只是将res 用作累加器,因此您可以添加score[letter](如果存在)或0(如果不存在)。这很容易使用dict.get 方法。

def compare(word):
    res = 0
    for letter in word:
        res += score.get(letter, 0)
        # dict.get(key, defaultvalue)
    return res

其实你甚至可以把它变成丑陋的lambda

compare = lambda word: sum([scores.get(letter,0) for letter in word])

【讨论】:

    【解决方案2】:
    score = {"a": 1, ...}
    
    def compare(word):
       res = 0
       for letter in word:
           if letter in score:
               res += score[letter]
       return res
    

    这可能就是你想要的。如果您确定所有字母都存在于score 中,您甚至可以省略if letter in score。你真的不需要比较任何东西。

    【讨论】:

      【解决方案3】:
      score = {"a": 1, "c": 3, "b": 3, "e": 1, "d": 2, "g": 2, 
       "f": 4, "i": 1, "h": 4, "k": 5, "j": 8, "m": 3, 
       "l": 1, "o": 1, "n": 1, "q": 10, "p": 3, "s": 1, 
       "r": 1, "u": 1, "t": 1, "w": 4, "v": 4, "y": 4, 
       "x": 8, "z": 10}
      
      def compare(word):
          res = 0
          for letter in word:
             if letter in score:
                res += score[letter]
          return res
      

      【讨论】:

        【解决方案4】:

        正如亚当的另一个回答所指出的那样,您正在以一种奇怪的方式思考这个问题。没有办法使用 score['*get_me_the_key*'] 之类的东西来获取字典分数的键。

        如果你真的想要钥匙。 score.keys() 将为您提供字典中的键列表。像这样的:

        keys = score.keys()
        print keys
        ['a', 'c', 'b', 'e', 'd', 'g', 'f', 'i', 'h', 'k', 'j', 'm', 'l', 'o', 'n', 'q', 'p', 's', 'r', 'u', 't', 'w', 'v', 'y', 'x', 'z']
        

        【讨论】:

          【解决方案5】:

          我在 Codecademy:Python 课程中发现了这个问题。在您正在做的问题中,您必须在函数的开头添加word = word.lower()。否则,如果您检查包含大写字母的单词,程序会出错。换句话说,“D”不在字典中,但“d”在。希望这会有所帮助。

          【讨论】:

            【解决方案6】:
            dict = {"a": 1, "c": 3, "b": 3, "e": 1, "d": 2, "g": 2, 
             "f": 4, "i": 1, "h": 4, "k": 5, "j": 8, "m": 3, 
             "l": 1, "o": 1, "n": 1, "q": 10, "p": 3, "s": 1, 
             "r": 1, "u": 1, "t": 1, "w": 4, "v": 4, "y": 4, 
             "x": 8, "z": 10}
            
            def compare(word):
            
            res = 0
            
            for letter in word:
            
               if letter in dict:
            
                  res += dict['letter']
            
            return res
            

            示例:

            dict['c']
            

            得到对应的值3

            【讨论】:

              猜你喜欢
              • 2018-10-05
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2021-03-04
              • 1970-01-01
              • 2017-02-20
              • 2010-10-06
              • 1970-01-01
              相关资源
              最近更新 更多