python,列表嵌套字典,获取字典(整个)数据
1、我有套列表嵌套字典的数据,我想把列表中的内容取出来,并且携带','。
2、数据模板如下:
list1 = [ {
"": "0",
"id": "4d834ba0525f451d88767696774d3b4a",
"name": "\u5347\u7ea7\u9879\u76ee005-OPT\u95ee\u9898\u4ee3\u7801",
"project_start_date": "",
"project_end_date": "2022-01-13",
"data_start_date": "2021-11-02",
"data_end_date": "2021-11-02",
"status": "3",
"create_user_id": "-200",
"create_date": "2022-01-13 15:52:19",
"update_date": "2022-01-13 16:08:53",
"type": "1",
"take_user_id": "-200",
"take_date": "2022-01-13 16:07:01",
"project_name_version": "0"
},
{
"": "1",
"id": "4d834ba0525f451d88767696774d3b4a",
"name": "\u5347\u7ea7\u9879\u76ee005-OPT\u95ee\u9898\u4ee3\u7801",
"project_start_date": "",
"project_end_date": "2022-01-13",
"data_start_date": "2021-11-02",
"data_end_date": "2021-11-02",
"status": "1",
"create_user_id": "-200",
"create_date": "2022-01-13 15:52:19",
"update_date": "2022-01-13 16:08:53",
"type": "2",
"take_user_id": "-200",
"take_date": "2022-01-13 16:07:01",
"project_name_version": "0"
},
{
"": "1",
"id": "4d834ba0525f451d88767696774d3b4a",
"name": "\u5347\u7ea7\u9879\u76ee005-OPT\u95ee\u9898\u4ee3\u7801",
"project_start_date": "",
"project_end_date": "2022-01-13",
"data_start_date": "2021-11-02",
"data_end_date": "2021-11-02",
"status": "1",
"create_user_id": "-200",
"create_date": "2022-01-13 15:52:19",
"update_date": "2022-01-13 16:08:53",
"type": "2",
"take_user_id": "-200",
"take_date": "2022-01-13 16:07:01",
"project_name_version": "0"
}
]
我想获得的效果是取出里边的字典,因为后续需要字典值作为入参,引入后续流程。
考虑过用列表遍历,但是发现遍历后。虽然能获得数据,但是就变成了str,并且内容被拆分了
回答:
我没有看明白你需要的是什么数据
遍历列表得到的是dict对象,而不应该是str
python">list1 = [{'id':0,'name':'jack'},{'id':1,'name':'ma'}]for i in list1:
print(i)
输出为:
{'id': 0, 'name': 'jack'}{'id': 1, 'name': 'ma'}
你要确定你的数据是list嵌套dict.而不是str类型的(json).你type(list1)
,type(list1[0])
确定数据类型.
传递字典
def func(kw): for key,value in kw.items():
print('key:%s---value:%s' %(key,value))
for i in list1:
func(i)
---------
输出:
key:id---value:0
key:name---value:jack
key:id---value:1
key:name---value:ma
解包:
def unpack_func(id,name): print('id:%s---name:%s' %(id,name))
for i in list1:
unpack_func(**i)
----------
输出:
id:0---name:jack
id:1---name:ma
回答:
for d in list1: print(type(d)) # 应为dict(前前后后加上的一大堆我忘了)
print(d)
搞不懂就调试
以上是 python,列表嵌套字典,获取字典(整个)数据 的全部内容, 来源链接: utcz.com/p/938261.html