简体   繁体   English

python-如何将字典的键与字符串字符进行比较

[英]python - how to compare a key of the dictionary with a string character

I am new to python and I need to compare a character in string with a key in a dictionary. 我是python的新手,我需要将字符串中的字符与字典中的键进行比较。 But I am not able to figure out a way to compare that character with a key. 但是我无法找到一种方法来比较该人物和钥匙。 I am only able to compare it with the value at dict[key] 我只能将其与dict [key]上的值进行比较

I am trying to implement something like this: 我正在尝试实现这样的事情:

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

where score[key] represents the value at that particular key as a whole. 其中score [key]代表整个特定键的值。 Is there a way to compare a key to the letter, instead of the value? 有没有办法将键和字母而不是值进行比较?

My aim is to compare the "letter" in "word" with the keys in dictionary and add the values against the characters and return the result. 我的目的是将“单词”中的“字母”与字典中的键进行比较,然后将值与字符相加并返回结果。

Looks like you're thinking about this strangely. 看起来您在想这个奇怪。 All you need to do is check if the letter is in your score dict, and if it is, to add that number to your total. 您所需要做的就是检查字母是否在score字典中,如果是,请将其加到总数中。

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

However there's an easier way to do this. 但是,有一种更简单的方法可以做到这一点。 Since you're just using res as an accumulator, you can add score[letter] if it exists or 0 if it doesn't. 由于您只是将res用作累加器,因此可以添加score[letter]如果存在)或添加0如果不存在)。 This is easy using the dict.get method. 使用dict.get方法很容易。

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

In fact you can even make it into an ugly lambda . 实际上,您甚至可以将其变成丑陋的lambda

compare = lambda word: sum([scores.get(letter,0) for letter in word])
score = {"a": 1, ...}

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

That's probably what you want. 那可能就是你想要的。 You can even omit if letter in score if you're sure all your letters will exist in score . 如果您确定所有字母都存在于score则甚至可以省略if letter in score的字母。 You don't really need to compare anything. 您实际上不需要比较任何东西。

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

As the other answer by Adam has pointed out you are thinking about this in a strange way. 正如亚当的另一个答案所指出的那样,您正在以一种奇怪的方式来思考这个问题。 There is NO way to get the key of the dictionary score using something like score['*get_me_the_key*'] . 无法使用诸如score['*get_me_the_key*']类的方法来获取字典分数的键。

If you really want the keys. 如果您真的想要钥匙。 score.keys() will give you a list of keys in the dictionary. score.keys()将为您提供字典中的键列表。 Something like this: 像这样:

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

example: 例:

dict['c']

get corresponding value 3 得到对应的值3

I recognize this problem in the Codecademy: Python course. 我在Codecademy:Python课程中认识到此问题。 In the problem you are doing, you must add word = word.lower() at the start of the function. 在您遇到的问题中,必须在函数的开头添加word = word.lower() Otherwise, if you check for words containing capital letters, the program will get errors. 否则,如果您检查包含大写字母的单词,程序将出错。 In other words, "D" is not in the dictionary, but "d" is. 换句话说,“ D”不在字典中,而“ d”在字典中。 Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM