Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中将3个列表合并成一个字典列表_Python_List_Dictionary - Fatal编程技术网

在python中将3个列表合并成一个字典列表

在python中将3个列表合并成一个字典列表,python,list,dictionary,Python,List,Dictionary,我刚开始学习Python,而且是个十足的傻瓜。所以我要做的是将3个列表合并到一个字典列表中 country= ["Japan", "Malaysia", "Philippine", "Thailand"] capital = ["Tokyo", "Kuala Lumpur", "Manila", "Bangkok"] currency = ["Yen", "Ringgit", "Peso", "Bath"] 以这种格式输入到列表中: data = [{"country":"Japan", "c

我刚开始学习Python,而且是个十足的傻瓜。所以我要做的是将3个列表合并到一个字典列表中

country= ["Japan", "Malaysia", "Philippine", "Thailand"]
capital = ["Tokyo", "Kuala Lumpur", "Manila", "Bangkok"]
currency = ["Yen", "Ringgit", "Peso", "Bath"]
以这种格式输入到列表中:

data = [{"country":"Japan", "capital":"Tokyo", "currency":"Yen"}, {"country":"Malaysia", "capital":"Kuala Lumpur", "currency":"Ringgit"}, {"country":"Philippine", "capital":"Manila", "currency":"Peso"},{"country":"Thailand", "capital":"Bangkok", "currency":"Bath"}]
Capital of Japan is Tokyo, and the currency is Yen.
Capital of Malaysia is Kuala Lumpur, and the currency is Ringgit.
Capital of Philippine is Manila, and the currency is Peso.
Capital of Thailand is Bangkok, and the currency is Bath.
然后以以下格式打印新列表:

data = [{"country":"Japan", "capital":"Tokyo", "currency":"Yen"}, {"country":"Malaysia", "capital":"Kuala Lumpur", "currency":"Ringgit"}, {"country":"Philippine", "capital":"Manila", "currency":"Peso"},{"country":"Thailand", "capital":"Bangkok", "currency":"Bath"}]
Capital of Japan is Tokyo, and the currency is Yen.
Capital of Malaysia is Kuala Lumpur, and the currency is Ringgit.
Capital of Philippine is Manila, and the currency is Peso.
Capital of Thailand is Bangkok, and the currency is Bath.

您只需使用
zip
进行简单的列表理解即可:

data = [{'country':coun, 'capital': cap, 'currency': curr} for coun, cap, curr in zip(country, capital, currency)]
然后,您可以遍历
数据
,并按如下方式打印:

for dictionary in data:
    print("Capital of {0} is {1} and currency is {2}".format(dictionary['country'],dictionary['capital'], dictionary['currency']))

可读性提示 请注意,与括号内的所有结构一样,您可以将列表拆分为多行:

data = [{'country':coun, 'capital': cap, 'currency': curr}
        for coun, cap, curr in zip(country, capital, currency)
       ]
您可能还希望将字符串保存为变量格式(可能使用其他常量),以便它不会隐藏在其他语句中:

message = "Capital of {0} is {1} and currency is {2}"
for dictionary in data:
    print(message.format(dictionary['country'],dictionary['capital'], dictionary['currency']))
您还可以在格式空间中指定键,而不是
{0}{1}
,然后使用
.format\u map
将键从字典映射到字符串:

message = "Capital of {country} is {capital} and currency is {currency}"
for dictionary in data:
    print(message.format_map(dictionary))

您只需使用
zip
进行简单的列表理解即可:

data = [{'country':coun, 'capital': cap, 'currency': curr} for coun, cap, curr in zip(country, capital, currency)]
然后,您可以遍历
数据
,并按如下方式打印:

for dictionary in data:
    print("Capital of {0} is {1} and currency is {2}".format(dictionary['country'],dictionary['capital'], dictionary['currency']))

可读性提示 请注意,与括号内的所有结构一样,您可以将列表拆分为多行:

data = [{'country':coun, 'capital': cap, 'currency': curr}
        for coun, cap, curr in zip(country, capital, currency)
       ]
您可能还希望将字符串保存为变量格式(可能使用其他常量),以便它不会隐藏在其他语句中:

message = "Capital of {0} is {1} and currency is {2}"
for dictionary in data:
    print(message.format(dictionary['country'],dictionary['capital'], dictionary['currency']))
您还可以在格式空间中指定键,而不是
{0}{1}
,然后使用
.format\u map
将键从字典映射到字符串:

message = "Capital of {country} is {capital} and currency is {currency}"
for dictionary in data:
    print(message.format_map(dictionary))

您缺少结尾的方括号!你能跟我开玩笑,给我一个分为两行的替代版本吗?建议每行最多79个字符,你已经超过了这一点。所以基本上我只需要用zip做一个列表理解,如果我想合并多个列表@Akhsat Mahajan非常感谢你提供的解决方案。@ TadhgMcDonald Jensen,我真的不确定在列表理解的中间做<代码> /<代码>是正确的。如果您有时间,我将非常感谢您的编辑。@Tadhgcdonald Jensen谢谢!您缺少结尾的方括号!你能跟我开玩笑,给我一个分为两行的替代版本吗?建议每行最多79个字符,你已经超过了这一点。所以基本上我只需要用zip做一个列表理解,如果我想合并多个列表@Akhsat Mahajan非常感谢你提供的解决方案。@ TadhgMcDonald Jensen,我真的不确定在列表理解的中间做<代码> /<代码>是正确的。如果您有时间,我将非常感谢您的编辑。@Tadhgcdonald Jensen谢谢!顺便说一句,泰国货币是泰铢,不是巴斯:)顺便说一句,泰国货币是泰铢,不是巴斯:)