python - 如何将用户输入与字典的键进行比较?
问题描述
我试图弄清楚如何将我的输入与字典的键进行比较。我想用字典和它们的值打印出匹配的单词。如果有人可以花一些时间来帮助我解决我的问题,那就太好了:
dictionary = {"nice":"0.5012", "awesome":"0.5766", "thankfull":"0.5891"}
def PNV(saysomething):
for token in nlp(saysomething):
while True:
if token in dictionary:
print("the word", key, "is same as" + str(token) + 'with the value' + dictionary[value])
解决方案
dictionary = {"nice":"0.5012", "awesome":"0.5766", "thankfull":"0.5891"}
def PNV(saysomething):
for token in nlp(saysomething):
if token in dictionary:
key = str(token)
value = str(dictionary[token])
print("the word", key, "is same as" + str(token) + 'with the value' + value)
编辑 基于 OP 评论
dictionary = {"nice":"0.5012", "awesome":"0.5766", "thankfull":"0.5891"}
def nlp(x):
return x.split()
def PNV(saysomething):
for token in nlp(saysomething):
if token in dictionary:
key = token
value = dictionary[token]
print("the word '{key}' is same as '{token}' with the value {value}".format(key = key, token = token, value = value))
PNV('trying to get a nice result, this is awesome')
生产
the word 'nice' is same as 'nice' with the value 0.5012
the word 'awesome' is same as 'awesome' with the value 0.5766
推荐阅读
- c# - 如何将 .OrderBy 用于多个条件,有时只使用一个?
- android - Room Database SQLite - 更新的阈值是多少?尝试循环执行 100 次更新
- php - 使用 --platform 和 php fpm-alpine 的 docker 文件 alpine 的正确语法
- javascript - JavaScript Uncaught TypeError - 购物车总数
- python - 如何从 .kv 文件添加在 .py 文件中制作的 Kivy 下拉菜单?
- python - 尝试使用 tkinter 和 opencv_cv2 (python) 构建 GUI 时出错
- javascript - 如何将 Javascript 数组字符串化为 JSON?
- loops - 是否可以使用 VST 或 Dafny 为字符串到 int 方法编写循环不变量?
- lua - Lua / FiveM,为什么获取散列对象很慢?
- reactjs - 如何将 Material UI Temporary Drawer 按钮设置为图标而不是文本?