【问题标题】:Testing for key in dictionary测试字典中的键
【发布时间】:2017-05-29 15:27:17
【问题描述】:

我正在尝试使用 Pyautogui 为名为“Graal Online Classic”的 MMO 创建一个机器人。它会读取向他们发送消息的玩家的“PM”,然后以适当的响应进行回复。它还将截取玩家姓名的屏幕截图,将其转换为带有 tesseract 的字符串,并将其存储在字典中(并且还存储该特定玩家在机器人的响应层次结构中的位置)并使用它来确定玩家是否已发送消息他们之前。

def player_id(name_coords, option): # Indentifies person who messaged and depending on if this person has messaged before changes response
    players = {} # Used to store players names and where in the response is
    keys = players.keys()

    image = pyautogui.screenshot('name_test.png', region = name_coords) # Names are screenshots 
    name_image = str(pytesseract.image_to_string(Image.open('name_test.png')))
    print(name_image)
    for name_image in keys:
        print("User is previous user.")
        if players[name_image] == None:
            players[name_image] = option
        else:
            players[name_image] = players[name_image] + option
        return players[name_image]
    else:
        print("User was not previously found, adding to dictionary.")
        players[name_image] = None
        print(players.keys())
        print(players)

问题源于上面的函数。当它找到以前给他们发过消息的人时,应该去 if 语句,但无论如何,else 语句都会执行。当玩家第二次向他们发送消息时,我打印玩家中的值,但对于 name_image 中的键:仍然返回 false。

如果需要,这里是完整代码https://pastebin.com/haJRfqJD

这是程序运行 3 条消息的日志

None
(454, 222)
Found a waiting message
Found an open message
1003 424
Player messaged 1
1
I hate sand
User was not previously found, adding to dictionary.
dict_keys(['I hate sand'])
{'I hate sand': None}
The bounty quest allows you to hunt mobs for cash! Location: Castle, steward's room(to the right in the throne room) [PM 1 for specifics, PM 2 for TIPS, PM 3 for possible bounties]
Replying...
(454, 222)
Found a waiting message
Found an open message
1003 424
Player messaged 1
1
I hate sand
User was not previously found, adding to dictionary.
dict_keys(['I hate sand'])
{'I hate sand': None}
The bounty quest allows you to hunt mobs for cash! Location: Castle, steward's room(to the right in the throne room) [PM 1 for specifics, PM 2 for TIPS, PM 3 for possible bounties]
Replying...
(454, 222)
Found a waiting message
Found an open message
1003 424
Player messaged 1
1
I hate sand
User was not previously found, adding to dictionary.
dict_keys(['I hate sand'])
{'I hate sand': None}
The bounty quest allows you to hunt mobs for cash! Location: Castle, steward's room(to the right in the throne room) [PM 1 for specifics, PM 2 for TIPS, PM 3 for possible bounties]
Replying...

【问题讨论】:

  • 如果您要检查密钥是否存在,请使用if not name_image in players:
  • name_image 取自 players.keys()。在什么情况下与None的值挂钩?

标签: python python-3.x dictionary


【解决方案1】:

这里不需要 for 循环。这就是使用字典的全部意义——避免昂贵的 O(N) 查找。字典查找速度很快。用

替换你的for循环
name = players.get(name_image)
if name:    
    # your variable naming is totally unclear. I **think** this is what you want
    players[name] = option
    return players[name]
else:
    print("User was not previously found, adding to dictionary.")
    players[name_image] = None
    print(players.keys())
    print(players)

请注意,在您的原始 cdoe 中,您正在定义一个 name_image,如下所示:

name_image = str(pytesseract.image_to_string(Image.open('name_test.png')))

但是那个变量在 for 循环中被覆盖了。

【讨论】:

  • 感谢您的帮助,但似乎 if name: 只是不想执行。这是我向自己发送 3 条同名消息时的日志。找不到数字答案我讨厌沙用户以前没有找到,添加到字典中。 dict_keys(['我讨厌沙子']) {'我讨厌沙子': 无} 无 我讨厌沙子 以前没有找到用户,添加到字典中。 dict_keys(['我讨厌沙子']) {'我讨厌沙子': None} 由于字符限制,我不能真正发布整个日志,但是从上面可以看出,即使值应该在字典里。
  • 我回复的太早了,请再检查一下。
  • 将日志添加到主帖。
  • 查看答案的更新。如前所述。您的 name_image 变量被严重滥用。由您决定哪个变量属于哪个位置。此答案解决了您发布的原始问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-09
  • 2017-04-03
  • 2023-04-04
  • 1970-01-01
  • 2022-01-25
  • 2017-08-10
相关资源
最近更新 更多