【问题标题】:Print Dictionary variables [closed]打印字典变量[关闭]
【发布时间】:2019-02-01 14:56:33
【问题描述】:
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: )

我希望它打印如下内容:

Sentence: I bought some Velcro shoes.
I bought some hook and loop fastener shoes.

【问题讨论】:

  • 那个作业不是几周前就出来了吗?你在化妆吗?
  • print(' '.join(BRANDS.get(i, i) for i in sentence.split()))
  • 你应该以鞋品牌为输入,从dict中获取key并造句。
  • ' '.join(BRANDS.get(val,val) for val in sentence.split(' '))
  • @U9-转发很酷,谢谢。

标签: python string python-3.x dictionary replace


【解决方案1】:

同意@Burhan 的想法,使用这个:

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

也可以使用 for 循环:

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

【讨论】:

猜你喜欢
  • 2016-06-07
  • 2019-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-18
  • 1970-01-01
  • 2018-08-04
相关资源
最近更新 更多