亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從python3中的嵌套字典列表中打印項目

從python3中的嵌套字典列表中打印項目

蠱毒傳說 2021-06-16 15:01:11
我有下面這樣的數據,我如何找到 Kevin 和 Jeremy 在 Python3 中擁有的 tool_id。所以我想要這樣的結果:4734238, 383744734233, 38374這是清單:assignment = [{    'id': 4734238,    'name': 'kevin',    'tool': {        'tool_id': '38374',        'new_tab': False,        'resource_link_id': '179fd40f91a5b'    }},{    'id': 4734233,    'name': 'jeremy',    'tool': {        'tool_id': '38374',        'new_tab': False,        'resource_link_id': '179fd40f91a5b'    },},{    'id': 4743233,    'name': 'Sharon',}]
查看完整描述

3 回答

?
長風秋雁

TA貢獻1757條經驗 獲得超7個贊

這將獲得所有ids和tool_ids擁有它們的人


l = [(person['id'], person['tool']['tool_id']) for person in assignment 

        if 'id' in person and 'tool' in person and 'tool_id' in person['tool']]


for pair in l:

    print(*pair, sep=', ')

印刷


4734238, 38374

4734233, 38374


查看完整回答
反對 回復 2021-06-22
?
尚方寶劍之說

TA貢獻1788條經驗 獲得超4個贊

assignment = [

{ 'id': "1001",'name': "kevin", 'tool': {'tool_id': '38374'}},

{ 'id': "1002", 'name': "jeremy", 'tool': {'tool_id': '38378'}}

]


findThis = ['kevin', 'jeremy']


for each in assignment:

    if each.get('name') in findThis:

        t = each.get('tool')

        print(each.get('id'), t.get('tool_id'))

我希望它更容易理解。


查看完整回答
反對 回復 2021-06-22
?
慕桂英546537

TA貢獻1848條經驗 獲得超10個贊

這應該這樣做:

[[i.get('id'), i.get('tool').get('tool_id')] for i in assignment if i.get('name') in ['kevin','jeremy']]

對于此示例,返回:

[[4734238, '38374'], [4734233, '38374']]


查看完整回答
反對 回復 2021-06-22
  • 3 回答
  • 0 關注
  • 113 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號