Python 怎么遍历字典转换格式?

Python 怎么遍历字典转换格式?

把下面这个字典格式,进行转换成新的字典格式,请教各位大佬

arr={'index': ['2022-07-14 18:00:00','2022-07-14 18:10:00','2022-07-14 18:20:00'],

'columns': ['一班', '二班', '三班'],

'data': [[0.0, 102.0, 44.0, 76.0, 0.0, 0.0, 0.0, 1.0],

[2.0, 242.0, 86.0, 110.0, 0.0, 5.0, 2.0, 1.0],

[2.0, 504.0, 150.0, 582.0, 0.0, 5.0, 2.0, 1.0]]}

新的格式:

newArr = [

{

cdate: "2022-07-14 18:00:00",

cname: "一班, 二班, 三班"

cut: "0.0, 102.0, 44.0, 76.0, 0.0, 0.0, 0.0, 1.0"

},

{

cdate: "2022-07-14 18:10:00",

cname:"一班, 二班, 三班",

cut: "2.0, 242.0, 86.0, 110.0, 0.0, 5.0, 2.0, 1.0"

},

{

cdate: "2022-07-14 18:20:00",

cname:"一班, 二班, 三班",

cut: "2.0, 504.0, 150.0, 582.0, 0.0, 5.0, 2.0, 1.0]"

}]


回答:

python">[{"cdata": arr["index"][i], "cname": arr["columns"][i], "cut": arr["data"][i]} for i in range(0, len(arr["index"]))]

以上是 Python 怎么遍历字典转换格式? 的全部内容, 来源链接: utcz.com/p/938519.html

回到顶部