Reputation: 29
I want to set up a python script that will take 2 inputs (a set of key value pairs & a nested list). I want to iterate the set of key value pairs through each item of the nested list, updating the list with the appropriate new values within the key value pairs. The nested list is a set of menu data. Example:
menu_data = [['mcdonalds', 'Lunch', 'Burgers', 'Big Mac', '10.95'],['mcdonalds', 'Lunch', 'Burgers', 'Quarter Pounder', '6.95'],['mcdonalds', 'Lunch', 'Burgers', 'Big Mac', '10.95']['mcdonalds', 'Lunch', 'Burgers', 'Bacon Cheeseburger', '12.95']]
The key:value pairs will also be contained in a nested list to start. They are price updates for the below items. Example:
updates = [['Big Mac', '9.95'], ['Quarter Pounder', '8.95'],['Bacon Cheeseburger', '11.95']]
Can anyone provide a nudge in the right direction as to the best functions/strategies/techniques to approach this problem. Having trouble getting started. Thanks for any help!
Upvotes: 1
Views: 6205
Reputation: 236
I use the below function to do a lookup for the desired key and return its value. So, I iterate over the list and for each dict within that list, I call this function to determine if it's the item (dict) that I'm looking for. Then I'll perform the action needed (update, send to template, etc.).
def find_value(dic, key):
""" return the value of dictionary dic given the key """
if key in dic:
return dic[key]
else:
return "key:'" + key + "'is not a valid key"
I realize this may not directly answer your question, but it might spur some thought that will get you to where you want to be. Good luck!
Upvotes: 1
Reputation: 12069
I suggest that you re-structure your menu data structure to simplify future updates and maintenance.
menu_data = [['mcdonalds', 'Lunch', 'Burgers', 'Big Mac', '10.95'],['mcdonalds', 'Lunch', 'Burgers', 'Quarter Pounder', '6.95'],['mcdonalds', 'Lunch', 'Burgers', 'Big Mac', '10.95']['mcdonalds', 'Lunch', 'Burgers', 'Bacon Cheeseburger', '12.95']]
It is very difficult to maintain such a list. For example, what if you wanted to retrieve all the burgers from your menu, how many iteration and comparisons do you think you will do?
How about using a dictionary
(link):
>>> menu_data = {'mcdonalds' : { 'Lunch' : { 'Burgers': {
'Big Mac': 10.95,
'Quarter Pounder': 6.95,
'Bacon Cheeseburger': 12.95
}
}
}
}
>>> menu_data
{'mcdonalds': {'Lunch': {'Burgers': {'Big Mac': 10.95, 'Bacon Cheeseburger': 12.95, 'Quarter Pounder': 6.95}}}}
>>> menu_data.get('mcdonalds').get('Lunch').get('Burgers')['Big Mac']
10.95
>>> menu_data.get('mcdonalds').get('Lunch').get('Burgers')['Big Mac'] = 9.95
>>> menu_data.get('mcdonalds').get('Lunch').get('Burgers')['Big Mac']
9.95
In that case, you could go through your updates one by one, and easily update each value.
Upvotes: 0
Reputation: 3774
Your list is just an array of arrays.
Here is a better example.
menu_data = [ { "name":"big mac", "price":"10.95"}, { "name":"Quarter Pounder", "price":"4.55"} ]
Now you have an array of items.
To iterate this list of items, you do f.ex :
for item in menu_data:
print "%s .... %i" % (item["name"], item["price"])
To add a happy burger to the list :
item = { "name":"Happy Burger", "price":"3.99" }
menu_data.append(item)
Just to explain a little about the items. Each item as done above is any number of key value dictionaries.
Key => value
so :
item = {"name":"John Taylor", "band":"Duran Duran"}
as each item can have any number of keys, you can access them with
all_keys = item.keys()
This will give you an array of ["name", "band"] Knowing that, you can iterate each item like :
for key in item:
print "The key is %s and the item is : %s" % (key, item[key])
I hope this helps you on your way to become better in Python.
Upvotes: 1