简体   繁体   中英

How to extract values from dictionary and format them into a sentence

This is what I have to do for this problem and I am not sure of the format. This is based on python 3.4 so f' strings cannot be used.

Here is the problem:

Create a function that takes a dictionary as an argument and returns a string with facts about the city. The city facts will need to be extracted from the dictionaries three properties:

name
population
continent

The string should have the following format: X has a population of Y and is situated in Z (where X is the city name, Y is the population and Z is the continent the city is situated in). Examples

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"*

This is what I originally came up with, but it doesn't work because Python 3.4 does not store order of dictionary values.

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

How could I fill in the blanks based on the dictionary values? The above code doesn't work because python 3.4 doesn't store order of dictionary values. What would I have to do to solve this problem based on Python 3.4?

you can directly access dictionary with keys like this

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))

Output:

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

You can use keyword arguments in 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))

Prints:

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

maybe this code can help you. I deconstruct your dictionary into a list, then I do a map function to make a string each of dictionary. This is the code.

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)

f-strings

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM