【问题标题】:How to extract values from dictionary and format them into a sentence如何从字典中提取值并将它们格式化为句子
【发布时间】:2020-06-30 22:03:24
【问题描述】:

这是我必须为这个问题做的,我不确定格式。这是基于 python 3.4 的,所以不能使用 f' 字符串。

问题来了:

创建一个函数,该函数将字典作为参数并返回一个包含有关城市的事实的字符串。城市事实需要从字典中提取三个属性:

name
population
continent

字符串应具有以下格式:X 的人口为 Y,并且位于 Z(其中 X 是城市名称,Y 是人口,Z 是城市所在的大陆)。 例子

city_facts({
  name: "Paris",
  population: "2,140,526",
  continent: "Europe"
}) ➞ "Paris has a population of 2,140,526 and is situated in Europe"
city_facts({
  name: "Tokyo",
  population: "13,929,286",
  continent: "Asia"
}) ➞ "Tokyo has a population of 13,929,286 and is situated in Asia"*

这是我最初想出的,但它不起作用,因为 Python 3.4 不存储字典值的顺序。

def city_facts(city):
    info = list(city.values())
    return '{} has a population of {} and is situated in {}'.format(info[0], info[2], info[1])

如何根据字典值填空?上面的代码不起作用,因为 python 3.4 不存储字典值的顺序。基于 Python 3.4 解决这个问题我该怎么做?

【问题讨论】:

  • python 3.4 于 6 年前发布 - 也许更新 python?
  • 告诉Edabit。
  • 为什么不直接将 info[0] 等替换为 city['name'] 等?
  • @pververis 太糟糕了,python 3.4 去年达到了 EOL!对此感到抱歉。
  • 他们真的需要在那个网站上参与并更新它!!!

标签: python dictionary key-value-store


【解决方案1】:

您可以使用这样的键直接访问字典

city = {"name": "Paris", "population": "2,140,526", "continent": "Europe"}


def city_facts(city):
    return "{} has a population of {} and is situated in {}".format(
        city["name"], city["population"], city["continent"]
    )

print(city_facts(city))

输出:

Paris has a population of 2,140,526 and is situated in Europe

【讨论】:

    【解决方案2】:

    您可以在str.format 中使用关键字参数:

    city = {"name": "Paris", "population": "2,140,526", "continent": "Europe"}
    print("{name} has a population of {population} and is situated in {continent}".format(**city))
    

    打印:

    Paris has a population of 2,140,526 and is situated in Europe
    

    【讨论】:

      【解决方案3】:

      也许这段代码可以帮助你。 我将您的字典解构为一个列表,然后我执行一个映射函数来为每个字典创建一个字符串。这是代码。

      city_facts = [{
        "name": "Paris",
        "population": "2,140,526",
        "continent": "Europe"
      },
      {
        "name": "Tokyo",
        "population": "13,929,286",
        "continent": "Asia"
      }]
      # im not sure what version of python able to use format string like below
      result = list(map(lambda data: f'{data["name"]} has a population of {data["population"]} and is situated in {data["continent"]}', city_facts))
      # you can also use this format string for your version python 3.4
      result = list(map(lambda data: '{} has a population of {} and is situated in {}'.format(data["name"],data["population"],data["continent"]), city_facts))
      # the result will be a list type, if you want to print as a single string, you can make an iteration to the list
      print(result)
      

      【讨论】:

        【解决方案4】:

        f-字符串

        city = {"name": "Paris", "population": "2,140,526", "continent": "Europe"}
        def city_facts(city):
            return f"{city["name"]} has a population of {city["population"]} and is situated in {city["continent"]}")
        print(city_facts(city))
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-05-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-06-15
          • 1970-01-01
          相关资源
          最近更新 更多