[英]Converting a list of dictionaries into a new dictionary
I'm trying to parse some html table content and I have a payload that looks like this:我正在尝试解析一些 html 表内容,并且我有一个如下所示的有效负载:
"payload": [
{
"Cell #1": "Origin",
"Cell #2": "Destination",
"Cell #3": "Miles",
"Cell #4": "Rate"
},
{
"Cell #1": "Pampa, TX",
"Cell #2": "Sallisaw, OK",
"Cell #3": "207",
"Cell #4": "$725"
},
{
"Cell #1": "Pampa, TX",
"Cell #2": "Sallisaw, OK",
"Cell #3": "207",
"Cell #4": "$725"
},
{
"Cell #1": "Pampa, TX",
"Cell #2": "Atoka, OK",
"Cell #3": "176",
"Cell #4": "$625"
},
{
"Cell #1": "Pampa, TX",
"Cell #2": "Wichita, KS",
"Cell #3": "460",
"Cell #4": "$1,150"
}
]
You can see that the first dictionary in this list is the table headers.您可以看到此列表中的第一个字典是表头。 Then every item after is ordered by its corresponding header.
然后之后的每个项目都由其相应的 header 订购。
I am trying to make it look like this:我试图让它看起来像这样:
[
{
"Origin": "Pampa, TX",
"Destination": "Sallisaw, OK",
"Miles": "207",
"Rate": "$725"
},
]
The catch is that the first list item which is the headers could change to add/remove "columns" so it would need to be versatile.问题是作为标题的第一个列表项可以更改为添加/删除“列”,因此它需要是通用的。
What I'm Trying First I believe that each dict should be converted to a list of their values:我首先尝试的是我相信每个 dict 都应该转换为它们的值列表:
rows = []
for row in rows:
rows.append(row.values)
This outputs list of lists:这将输出列表列表:
rows = [['Origin', 'Destination', 'Miles', 'Rate']), ['Pampa, TX', 'Sallisaw, OK', '207', '$725'], ['Pampa, TX', 'Sallisaw, OK', '207', '$725'], ['Pampa, TX', 'Atoka, OK', '176', '$625'], ['Pampa, TX', 'Wichita, KS', '460', '$1,150']]
normally if I was converting this and I knew the headers prior to receiving the payload, I would then be able to build the dictionary based on the preset headers.通常,如果我正在转换它并且我在接收有效负载之前知道标头,那么我将能够基于预设标头构建字典。
Something like:就像是:
for r in rows:
converted_row = {
"Origin": r[0],
"Destination": r[1],
"Miles": r[2],
"Rate": r[3]
}
But that doesn't solve for the part that the columns are dynamic.但这并不能解决列是动态的部分。 Also assuming that the ordering always matches.
还假设排序总是匹配的。 I have a feeling there is a more dynamic way to do this with list comprehension.
我感觉有一种更动态的方法可以通过列表理解来做到这一点。
If dicts
is your list of dictionaries and you are using at least Python 3.7 such that we can assume a deterministic order of dict items, then you can issue:如果
dicts
是您的字典列表,并且您至少使用 Python 3.7 以便我们可以假设 dict 项目的确定顺序,那么您可以发出:
>>> keys = dicts[0].values()
>>> [dict(zip(keys, d.values())) for d in dicts[1:]]
[{'Origin': 'Pampa, TX', 'Destination': 'Sallisaw, OK', 'Miles': '207', 'Rate': '$725'},
{'Origin': 'Pampa, TX', 'Destination': 'Sallisaw, OK', 'Miles': '207', 'Rate': '$725'},
{'Origin': 'Pampa, TX', 'Destination': 'Atoka, OK', 'Miles': '176', 'Rate': '$625'},
{'Origin': 'Pampa, TX', 'Destination': 'Wichita, KS', 'Miles': '460', 'Rate': '$1,150'}]
assuming you have this list stored in a variable named payload
...假设您将此列表存储在名为
payload
的变量中...
you can just grab the headers from the first entry and use them as keys for the values in the remaining entries, and turn them into a list of dictionaries using a list comprehension您可以从第一个条目中获取标题并将它们用作其余条目中值的键,然后使用列表推导将它们转换为字典列表
based on the OP I'm assuming you want a list of similar dictionaries for all entries:基于 OP 我假设您想要所有条目的类似词典列表:
headers = payload[0].values()
output = [dict(zip(headers,entry.values())) for entry in payload[1:]]
output: output:
[{'Destination': 'Sallisaw, OK',
'Miles': '207',
'Origin': 'Pampa, TX',
'Rate': '$725'},
{'Destination': 'Sallisaw, OK',
'Miles': '207',
'Origin': 'Pampa, TX',
'Rate': '$725'},
{'Destination': 'Atoka, OK',
'Miles': '176',
'Origin': 'Pampa, TX',
'Rate': '$625'},
{'Destination': 'Wichita, KS',
'Miles': '460',
'Origin': 'Pampa, TX',
'Rate': '$1,150'}]
you can use this with very easy to understand code你可以用非常容易理解的代码来使用它
#Declare an empty list to record the desired keys
keys=[]
#Filling the keys (dynamic and can be used for any other object)
for i in p[0].values():
keys.append(i)
#Declare an empty ans list
ans=[]
#Loop over the given payload values from 2nd value to last
for i in p[1:]:
#making the current ith dict values as a list
ls=list(i.values())
#declare an empty dictionary to catch the current iteration values
dict={}
#filling the current iteration dict values to the keys[]
for j in range(len(ls)):
dict[keys[j]]=ls[j]
#finally appending the current iteration temp dict to final ans list
ans.append(dict)
return ans
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.