payload = [
{
"Beds:": "3"
},
{
"Baths:": "2.0"
},
{
"Sqft:": "1,260"
},
]
我怎么会有这样的清单:
payload = [{'Beds':"3","Baths":"2.0","Sqft":"1,260"}]
而不是多个词典;我想在列表中找到一本字典。
答案 0 :(得分:0)
试试这个:
payload_new = [{i: j[i] for j in payload for i in j}]
答案 1 :(得分:0)
这应该有所帮助。使用replace
方法删除":"
payload = [
{
"Beds:": "3"
},
{
"Baths:": "2.0"
},
{
"Sqft:": "1,260"
},
]
newDict = [{k.replace(":", ""): v for j in payload for k,v in j.items()}]
print newDict
<强>输出:强>
[{'Beds': '3', 'Sqft': '1,260', 'Baths': '2.0'}]
答案 2 :(得分:0)
Python 3内置字典展开,试试这个
payload = {**payload_ for payload_ in payload}
答案 3 :(得分:0)
要在大字典中合并字典,您可以这样写:
payload={"Beds": 3 ,
"Baths": 2.0,
"Sqft": 1260
}
输出:
>>>payload["Baths"]
2.0
<强>视图强>:
[]
使其成为数组/列表而非字典。""
(例如:&#34; 3&#34;)使它们成为字符串而不是整数。