我有一个列表列表,我想转换成:
dict[i]['name']
dict[i]['stars']
dict[i]['numrevs']
dict[i]['price']
dict[0]
告诉我第一列数据(Gus 's World Famous Fried Chicken)
内部列表如下:
- 餐厅名称(name)
- 星级(星)
- 评论数(numrevs)
- 价格(价格)
bigList = [
['Gus’s World Famous Fried Chicken', 'South City Kitchen - Midtown', 'Mary Mac’s Tea Room', 'Busy Bee Cafe', 'Richards’ Southern Fried', 'Greens & Gravy', 'Colonnade Restaurant', 'South City Kitchen Buckhead', 'Poor Calvin’s', 'Rock’s Chicken & Fries', 'Copeland’s'],
['4.0', '4.5', '4.0', '4.0', '4.0', '3.5', '4.0', '4.5', '4.5', '4.0', '3.5'],
[549, 1777, 2241, 481, 108, 93, 350, 248, 1558, 67, 288],
['$$', '$$', '$$', '$$', '$$', '$$', '$$', '$$', '$$', '$', '$$']]
任何帮助都将非常感激。
- 使用
zip
转换列表 - 调用
dict
,将键和转置列表压缩在一起
那就变成
keys = ["name","stars","numReviews","price"]
myDicts = [dict(zip(keys, row)) for row in zip(*bigList)]
这基本上就是一个列表推导式它执行以下操作
dict([('name',"Some Name"),("stars", 5),("numReviews",50),("price","expensive")])
和将产生上述字典的列表
[{"name":"Some Name",...},{"name":"Other name",...}, ...]