在python中的列表中添加sub dictonary元素

时间:2017-12-21 11:55:56

标签: python

我正在尝试在列表中添加子字典元素。它给了我类型错误。 这是字典和我的代码:

{
    "key1": "value1",
    "key2": {
        "skey1": "svalue2",
        "skey2": {
            "sskey1": [{
                "url": "value",
                "sid": "511"
            },
                {
                    "url": "value",
                    "sid": "522"
                },
                {
                    "url": "value",
                    "sid": "533"
                }]
        }
    }
}

我想将sid添加到列表中,如[511,522,533]

这是我的代码:

rsId=[]
for i in op['key2']['skey2']['sskey1']:
    for k,v in i.items():
        if k=='sid':
            rsId.append(v)  

4 个答案:

答案 0 :(得分:2)

D = { 
    "key1":"value1",
    "key2":{ 
             "skey1":"svalue2",
             "skey2":{
                        "sskey1":[{ 
                                    "url":"value",
                                    "sid":"511"
                                  },
                                  { 
                                    "url":"value",
                                    "sid":"522"
                                  },
                                  { 
                                    "url":"value",
                                    "sid":"533"
                                  } ]
      }
  }   
}

res = []
for i in D['key2']['skey2']['sskey1']:
    res.append(i['sid'])
print res

结果:

['511', '522', '533']

或一行代码:

res = [i['sid'] for i in D['key2']['skey2']['sskey1']]

答案 1 :(得分:1)

你可以使用dict理解:

rsId = [v for item in op['key2']['skey2']['sskey1'] for k, v in item.items() if k == 'sid']

答案 2 :(得分:1)

你可以试试这样的一行:

print(list(map(lambda x:x['sid'],data['key2']['skey2']['sskey1'])))

输出:

['511', '522', '533']

如果你想在int中使用值,那么:

print(list(map(lambda x:int(x['sid']),data['key2']['skey2']['sskey1'])))

输出:

[511, 522, 533]

数据为:

data = {
    "key1":"value1",
    "key2":{
             "skey1":"svalue2",
             "skey2":{
                        "sskey1":[{
                                    "url":"value",
                                    "sid":"511"
                                  },
                                  {
                                    "url":"value",
                                    "sid":"522"
                                  },
                                  {
                                    "url":"value",
                                    "sid":"533"
                                  } ]
      }
  }
}

答案 3 :(得分:1)

获取int作为输出

  

类型错误可能是因为您将字符串作为列表项。让我们看看它将它转换为数字机智int()它解决了你的问题。   对代码的唯一更改是在最后一行代码中。

op = {
    "key1": "value1",
    "key2": {
        "skey1": "svalue2",
        "skey2": {
            "sskey1": [{
                "url": "value",
                "sid": "511"
            },
                {
                    "url": "value",
                    "sid": "522"
            },
                {
                    "url": "value",
                    "sid": "533"
            }]
        }
    }
}

rsId = []
for i in op['key2']['skey2']['sskey1']:
    for k, v in i.items():
        if k == 'sid':
            rsId.append(int(v)) # put the int here

输出

>>> rsId
[511, 522, 533]

另一种方法:检查每个具有字典值的键

op = {
    "key1": "value1",
    "key2": {
        "skey1": "svalue2",
        "skey2": {
            "sskey1": [
                {
                    "url": "value",
                    "sid": "511"
                },

                {
                    "url": "value",
                    "sid": "522"
                },

                {
                    "url": "value",
                    "sid": "533"
                }
            ]
        }
    }
}
l = []
for k in op: # searching in the main dictonary
    if type(op[k]) is dict: # if the value contains a dict (sub1)
        for k2 in op[k]:    # for every key
            if type(op[k][k2]) is dict: # if the value is a dict (sub2)
                for k3 in op[k][k2]: # for each key of subdict 2
                    for i in op[k][k2][k3]: # for every item of the list
                        for k4 in i: # foreach key in the item (a dict)
                            if k4 == 'sid': # if the key is 'sid'
                                l.append(int((i[k4]))) # append the value

print(l)

输出

[511, 522, 533]