将嵌套列表转换为列表内的字典



我有一个列表列表,我想转换成:

dict[i]['name']
dict[i]['stars']
dict[i]['numrevs']
dict[i]['price']

dict[0]告诉我第一列数据(Gus 's World Famous Fried Chicken)

内部列表如下:

  1. 餐厅名称(name)
  2. 星级(星)
  3. 评论数(numrevs)
  4. 价格(价格)
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],
['$$', '$$', '$$', '$$', '$$', '$$', '$$', '$$', '$$', '$', '$$']]

任何帮助都将非常感激。

  1. 使用zip转换列表
  2. 调用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",...}, ...]

相关内容

  • 没有找到相关文章

最新更新