python中的两个Json格式的列表 [英] two Lists to Json Format in python

查看:122
本文介绍了python中的两个Json格式的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个列表

a=["USA","France","Italy"]
b=["10","5","6"]

我希望最终结果为在这样的json中。

I want the end result to be in json like this.

[{"country":"USA","wins":"10"},
{"country":"France","wins":"5"},
{"country":"Italy","wins":"6"},
]

我使用zip(a,b)加入两个,但无法命名为

I used zip(a,b) to join two but couldn't name it

推荐答案

使用列表理解

>>> [{'country': country, 'wins': wins} for country, wins in zip(a, b)]
[{'country': 'USA', 'wins': '10'},
 {'country': 'France', 'wins': '5'},
 {'country': 'Italy', 'wins': '6'}]

使用 json.dumps 获取JSON:

Use json.dumps to get JSON:

>>> json.dumps(
...     [{'country': country, 'wins': wins} for country, wins in zip(a, b)]
... )
'[{"country": "USA", "wins": "10"}, {"country": "France", "wins": "5"}, {"country": "Italy", "wins": "6"}]'

这篇关于python中的两个Json格式的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆