Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 打印字典变量_Python_String_Python 3.x_Dictionary_Replace - Fatal编程技术网

Python 打印字典变量

Python 打印字典变量,python,string,python-3.x,dictionary,replace,Python,String,Python 3.x,Dictionary,Replace,我想让它打印如下内容: BRANDS = { 'Velcro': 'hook and loop fastener', 'Kleenex': 'tissues', 'Hoover': 'vacuum', 'Bandaid': 'sticking plaster', 'Thermos': 'vacuum flask', 'Dumpster': 'garbage bin', 'Rollerblade': 'inline skate', 'Aspirin': 'acetylsalicylic acid'

我想让它打印如下内容:

BRANDS = {
'Velcro': 'hook and loop fastener',
'Kleenex': 'tissues',
'Hoover': 'vacuum',
'Bandaid': 'sticking plaster',
'Thermos': 'vacuum flask',
'Dumpster': 'garbage bin',
'Rollerblade': 'inline skate',
'Aspirin': 'acetylsalicylic acid'
}
sentence = input (Sentence: )

同意@Burhan的想法,使用以下方法:

Sentence: I bought some Velcro shoes.
I bought some hook and loop fastener shoes.
也可以使用for循环:

sentence = input('Sentence: ')
print(' '.join(BRANDS.get(i,i) for i in sentence.split()))

那份作业不是几周前就出来了吗?你在化妆吗?
print(''.join(BRANDS.get(i,i)for i in sense.split())
你应该把鞋牌作为输入,从dict那里拿到钥匙,然后造句。''.join(BRANDS.get(val,val)for val in sense.split('))@U9 Forward cool谢谢。你知道吗?你的评论似乎是一致的,你的回答与想法形成了对比。
sentence = input('Sentence: ')
for i in sentence.split():
   sentence=sentence.replace(i,BRANDS.get(i,i))
print(sentence)