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)