简体   繁体   中英

python nested list to dictionary

what is the best way to convert this list into a dictionary (call it my_dict) so that it can be indexed this way?

my_dict[i]['name']
my_dict[i]['stars']
my_dict[i]['price']

Basically my_dict[0] would give me everything about 'CalaBar & Grill' .

Here's the list:

[['CalaBar & Grill', '4.0 star rating', '$$'],
 ['Red Chili Cafe', '4.0 star rating', '$$'],
 ['Gus’s World Famous Fried Chicken', '4.0 star rating', '$$'],
 ['South City Kitchen - Midtown', '4.5 star rating', '$$'],
 ['Mary Mac’s Tea Room', '4.0 star rating', '$$'],
 ['Busy Bee Cafe', '4.0 star rating', '$$'],
 ['Richards’ Southern Fried', '4.0 star rating', '$$'],
 ['Greens & Gravy', '3.5 star rating', '$$'],
 ['Colonnade Restaurant', '4.0 star rating', '$$'],
 ['South City Kitchen Buckhead', '4.5 star rating', '$$'],
 ['Poor Calvin’s', '4.5 star rating', '$$'],
 ['Rock’s Chicken & Fries', '4.0 star rating', '$'],
 ['Copeland’s', '3.5 star rating', '$$']]

You can zip the keys of the desired sub-dicts with the corresponding values in a dict constructor (assuming your list is stored in variable l ):

[dict(zip(('name', 'stars', 'price'), i)) for i in l]

This returns:

[{'name': 'CalaBar & Grill', 'stars': '4.0 star rating', 'price': '$$'}, {'name': 'Red Chili Cafe', 'stars': '4.0 star rating', 'price': '$$'}, {'name': 'Gus’s World Famous Fried Chicken', 'stars': '4.0 star rating', 'price': '$$'}, {'name': 'South City Kitchen - Midtown', 'stars': '4.5 star rating', 'price': '$$'}, {'name': 'Mary Mac’s Tea Room', 'stars': '4.0 star rating', 'price': '$$'}, {'name': 'Busy Bee Cafe', 'stars': '4.0 star rating', 'price': '$$'}, {'name': 'Richards’ Southern Fried', 'stars': '4.0 star rating', 'price': '$$'}, {'name': 'Greens & Gravy', 'stars': '3.5 star rating', 'price': '$$'}, {'name': 'Colonnade Restaurant', 'stars': '4.0 star rating', 'price': '$$'}, {'name': 'South City Kitchen Buckhead', 'stars': '4.5 star rating', 'price': '$$'}, {'name': 'Poor Calvin’s', 'stars': '4.5 star rating', 'price': '$$'}, {'name': 'Rock’s Chicken & Fries', 'stars': '4.0 star rating', 'price': '$'}, {'name': 'Copeland’s', 'stars': '3.5 star rating', 'price': '$$'}]

Here's a way to do it with a list comprehension, in vanilla python. Assuming the 2D list you gave is stored in my_list :

keys = ['name', 'stars', 'price']
my_dict = [dict(zip(keys, values)) for values in my_list]

The zip(k, v) takes two lists and maps them into a dictionary-like structure so that k is the keys, and each v is the corresponding values. You do need to cast the result to a dict , though.

This should work:

# The list
my_list = [['CalaBar & Grill', '4.0 star rating', '$$'], \
 ['Red Chili Cafe', '4.0 star rating', '$$'],\
 ['Gus’s World Famous Fried Chicken', '4.0 star rating', '$$'],\
 ['South City Kitchen - Midtown', '4.5 star rating', '$$'],\
 ['Mary Mac’s Tea Room', '4.0 star rating', '$$'],\
 ['Busy Bee Cafe', '4.0 star rating', '$$'],\
 ['Richards’ Southern Fried', '4.0 star rating', '$$'],\
 ['Greens & Gravy', '3.5 star rating', '$$'],\
 ['Colonnade Restaurant', '4.0 star rating', '$$'],\
 ['South City Kitchen Buckhead', '4.5 star rating', '$$'],\
 ['Poor Calvin’s', '4.5 star rating', '$$'],\
 ['Rock’s Chicken & Fries', '4.0 star rating', '$'],\
 ['Copeland’s', '3.5 star rating', '$$']]

# initialize an empty list
my_dict = []

# create list of dictionary
for elem in my_list:
    temp_dict = {}
    temp_dict['name'] = elem[0]
    temp_dict['stars'] = elem[1]
    temp_dict['price'] = elem[2]
    my_dict.append(temp_dict)


# testing
print(my_dict[1]['stars'])
print(my_dict[5]['price'])
print(my_dict[0]['name'])
print(my_dict[7]['stars'])

Using a simpler list comprehension, you can create the dict with:

list = [['CalaBar & Grill', '4.0 star rating', '$$'],
 ['Red Chili Cafe', '4.0 star rating', '$$'],
 ['Gus’s World Famous Fried Chicken', '4.0 star rating', '$$'],
 ['South City Kitchen - Midtown', '4.5 star rating', '$$'],
 ['Mary Mac’s Tea Room', '4.0 star rating', '$$'],
 ['Busy Bee Cafe', '4.0 star rating', '$$'],
 ['Richards’ Southern Fried', '4.0 star rating', '$$'],
 ['Greens & Gravy', '3.5 star rating', '$$'],
 ['Colonnade Restaurant', '4.0 star rating', '$$'],
 ['South City Kitchen Buckhead', '4.5 star rating', '$$'],
 ['Poor Calvin’s', '4.5 star rating', '$$'],
 ['Rock’s Chicken & Fries', '4.0 star rating', '$'],
 ['Copeland’s', '3.5 star rating', '$$']]

my_dict = {'venues': [{'name': item[0], 'stars': item[1], 'price': item[2]} for item in list]}

my_dict_entries = my_dict['venues']

for i in range(len((my_dict_entries))):
    print(my_dict_entries[i]['name'])
    print(my_dict_entries[i]['stars'])
    print(my_dict_entries[i]['price'])

dict :

{"venues": [{"name": "CalaBar & Grill", "rating": "4.0 star rating", "pricing": "$$"}, {"name": "Red Chili Cafe", "rating": "4.0 star rating", "pricing": "$$"}, {"name": "Gus\u2019s World Famous Fried Chicken", "rating": "4.0 star rating", "pricing": "$$"}, {"name": "South City Kitchen - Midtown", "rating": "4.5 star rating", "pricing": "$$"}, {"name": "Mary Mac\u2019s Tea Room", "rating": "4.0 star rating", "pricing": "$$"}, {"name": "Busy Bee Cafe", "rating": "4.0 star rating", "pricing": "$$"}, {"name": "Richards\u2019 Southern Fried", "rating": "4.0 star rating", "pricing": "$$"}, {"name": "Greens & Gravy", "rating": "3.5 star rating", "pricing": "$$"}, {"name": "Colonnade Restaurant", "rating": "4.0 star rating", "pricing": "$$"}, {"name": "South City Kitchen Buckhead", "rating": "4.5 star rating", "pricing": "$$"}, {"name": "Poor Calvin\u2019s", "rating": "4.5 star rating", "pricing": "$$"}, {"name": "Rock\u2019s Chicken & Fries", "rating": "4.0 star rating", "pricing": "$"}, {"name": "Copeland\u2019s", "rating": "3.5 star rating", "pricing": "$$"}]}

dict_entries :

[{'name': 'CalaBar & Grill', 'stars': '4.0 star rating', 'price': '$$'}, {'name': 'Red Chili Cafe', 'stars': '4.0 star rating', 'price': '$$'}, {'name': 'Gus’s World Famous Fried Chicken', 'stars': '4.0 star rating', 'price': '$$'}, {'name': 'South City Kitchen - Midtown', 'stars': '4.5 star rating', 'price': '$$'}, {'name': 'Mary Mac’s Tea Room', 'stars': '4.0 star rating', 'price': '$$'}, {'name': 'Busy Bee Cafe', 'stars': '4.0 star rating', 'price': '$$'}, {'name': 'Richards’ Southern Fried', 'stars': '4.0 star rating', 'price': '$$'}, {'name': 'Greens & Gravy', 'stars': '3.5 star rating', 'price': '$$'}, {'name': 'Colonnade Restaurant', 'stars': '4.0 star rating', 'price': '$$'}, {'name': 'South City Kitchen Buckhead', 'stars': '4.5 star rating', 'price': '$$'}, {'name': 'Poor Calvin’s', 'stars': '4.5 star rating', 'price': '$$'}, {'name': 'Rock’s Chicken & Fries', 'stars': '4.0 star rating', 'price': '$'}, {'name': 'Copeland’s', 'stars': '3.5 star rating', 'price': '$$'}]

Output [truncated]:

CalaBar & Grill
4.0 star rating
$$
Red Chili Cafe
4.0 star rating
$$
Gus’s World Famous Fried Chicken
4.0 star rating
$$
South City Kitchen - Midtown
4.5 star rating
$$
...

This will give you a more robust venues dict structure that allows you to address your list well. For example, my_dict_entries gives you the list your looking for in your question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM