基于主字典索引访问嵌套字典

时间:2018-03-05 23:57:10

标签: python dictionary

我在下面的字典(Photo_id)中有嵌套值的值,如何在同一索引处获取不同字典(即sample_id)的索引值?

我有一个字典里面有子词典:

ansible

我如何查看索引并给我那里的photo_desc值?

即。我有f_id 1366,我希望脚本告诉我photo_desc = C

我的代码如下:

per_row = organised.to_dict('index')
        {0: {'Photo_id': nan, 'Photo_Desc': nan, 'Sample_id': 20025521L},
         1: {'Photo_id': 1460, 'Photo_Desc': u'S', 'Sample_id': 20025523L},
         2: {'Photo_id': u'1366, 1367, 1368', 'Photo_Desc': u'C', 'Sample_id': 20025522L}}

输出:

for key, value in per_row.iteritems():
    print key, value
    for k,v in value.items():
        print k,v
        if k == 'Photo_id':
            v = str(v)
            if v == 'nan':
                pass
            else:
                print v
                print type(v)
                for fid in v.split():
                    print "fid " + fid
                    removedcommas = re.sub(',', '', fid)
                    print "Photo id cleaned: "+ removedcommas

                    *** Now check photo_desc:
                    if photo_desc == 'C':
                        do this
                    else photo_desc == 'S':
                        do something else

1 个答案:

答案 0 :(得分:0)

要获取photo_desc,只需访问value['Photo_Desc']即可。