小编wak*_*ude的帖子

Python-用字典中的条目替换字符串中的单词

我正在尝试制作一个程序,该程序将接受输入,查看这些单词中的任何一个是否是先前定义的字典中的键,然后用它们的条目替换任何找到的单词。难点在于“看看单词是否是关键”。例如,如果我尝试替换该字典中的条目:

dictionary = {"hello": "foo", "world": "bar"}
Run Code Online (Sandbox Code Playgroud)

当给定输入“hello world”时,如何使其打印“foo bar”?

python dictionary replace key input

2
推荐指数
1
解决办法
1万
查看次数

标签 统计

dictionary ×1

input ×1

key ×1

python ×1

replace ×1