【问题标题】:Get only one word from a dictonary in python从python中的字典中只获取一个单词
【发布时间】:2021-02-08 17:09:48
【问题描述】:
import COVID19Py

covid19 = COVID19Py.COVID19()
latest = covid19.getLatest()
print(latest)

这是我的代码,输出是:

{'confirmed': 106160999, 'deaths': 2317170, 'recovered': 12778299}

但我希望输出类似于:

"There are 106160999 infections and 2317170 deaths and 12778299 recovers"

【问题讨论】:

  • 这样基本的问题表明您可能会发现遵循一些有关 Python 中的字典和格式化字符串的教程会更有帮助。网上有很多这样的教程,Stack Overflow 不是其中之一。请使用tour,阅读what's on-topic hereHow to Askquestion checklist。欢迎来到堆栈溢出
  • 只从字典中得到一个单词,您想要的结果如何?看起来你想要所有的单词,只是用英文格式化。

标签: python dictionary output


【解决方案1】:
In [27]: latest = {'confirmed': 106160999, 'deaths': 2317170, 'recovered': 12778299}                                                                                                                                                                                          

In [28]: print(f"There are {latest['confirmed']} infections and {latest['deaths']} deaths and {latest['recovered']} recovers")                                                                                                                                                
There are 106160999 infections and 2317170 deaths and 12778299 recovers

【讨论】:

    【解决方案2】:

    如果你只是想访问字典,你可以这样写:

    print(f"There are {your_dict["confirmed"]} infections and ....")
    

    您可以像数组一样使用键访问字典

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-05
      • 2017-03-19
      相关资源
      最近更新 更多