如何从字典中提取值并将它们格式化成一个句子
How to extract values from dictionary and format them into a sentence
这是我必须为这个问题做的,我不确定格式。这是基于 python 3.4 所以不能使用 f' 字符串。
这是问题所在:
创建一个函数,将字典作为参数,returns 包含有关城市的信息的字符串。城市事实需要从字典中提取三个属性:
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,我需要做什么来解决这个问题?
你可以直接用这样的键访问字典
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
您可以在 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
也许这段代码可以帮到你。
我将你的字典解构为一个列表,然后我做了一个映射函数来为每个字典制作一个字符串。这是代码。
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))
这是我必须为这个问题做的,我不确定格式。这是基于 python 3.4 所以不能使用 f' 字符串。
这是问题所在:
创建一个函数,将字典作为参数,returns 包含有关城市的信息的字符串。城市事实需要从字典中提取三个属性:
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,我需要做什么来解决这个问题?
你可以直接用这样的键访问字典
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
您可以在 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
也许这段代码可以帮到你。 我将你的字典解构为一个列表,然后我做了一个映射函数来为每个字典制作一个字符串。这是代码。
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))