gpt4 book ai didi

python - 如何验证 python 中 dict 中某些值的某些分割值?

转载 作者:行者123 更新时间:2023-12-01 02:55:30 26 4
gpt4 key购买 nike

我正在尝试验证列表中的某些分割值是否在 python 中的 dict 中的某些值中。

例如,

user_input = ".."
morse_code = {"A": ".-", "N": "-.", "B": "-...", "O": "---", "C": "-.-.", "P": ".--.", "D": "-..", "Q": "--.-", "E": ".", "R": ".-.", "F": "..-.", "S": "...", "G": "--.", "T": "-", "H": "....", "U": "..-", "I": "..", "V": "...-", "K": "-.-", "X": "-..-", "J": ".---", "W": ".--", "L": ".-..", "Y": "-.--", "M": "--", "Z": "--.."}
>>> user_input in morse_code.values()
True
>>> user_input.split() in morse_code.values()
False

我以为user_input == user_input.split()但事实并非如此。这是为什么?

最佳答案

这是因为 split 方法返回一个元素列表。

>>> user_input.split()
['..']
>>> user_input
'..'

你的字典里没有这个列表。

关于python - 如何验证 python 中 dict 中某些值的某些分割值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44243151/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com