Python嵌套dict来自dicts列表

时间:2018-03-09 10:43:26

标签: python dictionary for-loop

我有这个清单:

[{"id": "11a6343897504c219ddec86491394b84", "smile_conf": 
  "99.91191864013672", "mouthopen_conf": "97.74097442626953", "smile":"False",
  "camid": "p2.jpeg"},{"id": "11a6343897504c219ddec864914444", "smile_conf": 
  "99.913333672", "mouthopen_conf": "97.74097442626953", "smile": "True", 
  "camid": "p2.jpeg"},{"id": "11a634344441394b84", "smile_conf": 
  "99.91191864013672", "mouthopen_conf": "97.74097442626953", "smile": "False",
  "camid": "p2.jpeg"}]

我想创建一个像这样的字典:

{
    {"id": "11a6343897504c219ddec86491394b84",
         {"smile_conf": "99.91191864013672",
             "mouthopen_conf": "97.74097442626953",
             "smile": "False",
             "camid": "p2.jpeg"}
    }
    {"id": "11a6343897504c219ddec864914444",
        {"smile_conf": "99.91191864013672",
            "mouthopen_conf": "97.74097442626953",
            "smile": "True",
            "camid": "p2.jpeg"}
    }
    {"id": "11a634344441394b84",
        {"smile_conf": "99.91191864013672",
            "mouthopen_conf": "97.74097442626953",
            "smile": "True",
            "camid": "p2.jpeg"}

}

我尝试:

mydict = {}
for i in range(0,len(l1)):
    mydict[i] = {}
    for id,cont in l1[i]:
         mydict[i][id] = cont

但它不起作用,它在嵌套for。

时返回错误

我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:1)

Dicts用于存储键值对,但您要为索引分配值,这是列表的用途。您不需要将列表转换为dict。 您可以像这样迭代列表:

for item in lst:
    <do something>

或者您可以使用整数索引列表:

lst[n]

但dicts用于存储键值对,如下所示:

dct = {}
dct["hello"] = 42
dct[5] = 786
for i in dct.keys(): # Iterate over the keys
    print(i, dct[i])

dict语法写成如下:

dct = {key: val, key2: val2, ...}

你不能有这样的字典:{0,1,2,3},因为dicts存储键值对。这是无法编制索引的集合的语法。

s = {1, 2, 3}
s[0] # ERROR

集合对于检查它们是否包含元素很有用。 还要注意,集合不能存储不可用(通常是可变的)对象。这也适用于dict键(但不适用于值)。所以你不能有一套词汇。

答案 1 :(得分:1)

如果您的预期输出是

{
  "11a6343897504c219ddec86491394b84": {
    "camid": "p2.jpeg",
    "smile": "False",
    "smile_conf": "99.91191864013672",
    "mouthopen_conf": "97.74097442626953"
  },
  "11a634344441394b84": {
    "camid": "p2.jpeg",
    "smile": "False",
    "smile_conf": "99.91191864013672",
    "mouthopen_conf": "97.74097442626953"
  },
  "11a6343897504c219ddec864914444": {
    "camid": "p2.jpeg",
    "smile": "True",
    "smile_conf": "99.913333672",
    "mouthopen_conf": "97.74097442626953"
  }
}

然后尝试下面简单的一个:

list = [
  {
    "id": "11a6343897504c219ddec86491394b84",
    "smile_conf": "99.91191864013672",
    "mouthopen_conf": "97.74097442626953",
    "smile": "False",
    "camid": "p2.jpeg"
  },
  {
    "id": "11a6343897504c219ddec864914444",
    "smile_conf": "99.913333672",
    "mouthopen_conf": "97.74097442626953",
    "smile": "True",
    "camid": "p2.jpeg"
  },
  {
    "id": "11a634344441394b84",
    "smile_conf": "99.91191864013672",
    "mouthopen_conf": "97.74097442626953",
    "smile": "False",
    "camid": "p2.jpeg"
  }
]

print list

requiredDict = {}

for eachElement in list:
    id = eachElement["id"]
    requiredDict[id] = {}
    del eachElement["id"]
    requiredDict[id] = eachElement

print requiredDict