Hello everyone,
I am absolutely new to Python and programming. Since my girlfriend always makes grocery lists, but completely unsorted, i thought this might be a good inital possibility to write a small program in which you could enter what you'd like to buy and the program throws out the items sorted by their position in the store.
I thought it could work like that: First I divide the store in 10 diffrent areas, where #1 is at the entrance and #10 is exit. Then I make a dictonary with all the products and assign them an area like
glist = dict ([('banana',1),('steak',5),('water',9),('cheese',4)])
But from this point I have no idea how to continue. I do not know how to sort the items and how i later make the input of the list.
Any ideas?
Many thanks
bionoob
Not sure what you're making here
glist = dict ([('banana',1),('steak',5),('water',9),('cheese',4)])
but a dictionary would be more like:
glist = {'banana':1, 'steak':5, 'water':9, 'cheese':4} # Note the curly braces
Why would you want to sort the dictionary?
Well, probably I shouldn't sort the dictonary, but I thought:
1)entering items that you want to buy
2)python looks up (in some sort of dictionary) in which store area (1-10) these items can be found
3)it returns you the entered items but now arranged in the order of their respective areas.
groceries = [('banana',1),('steak',5),('water',9),('cheese',4)]
groceries.sort(key = lambda x: x[1])
The key parameter to sort allows you to sort by the result of a function applied to each item. The lambda statement makes an unnamed function, in this case returning the second item of a sequence. So the above will sort your list by the second item in each tuple.
Where a dictionary would be useful is to store which aisle each item is in, so you don't have to enter it each time:
locations = {'banana': 1, 'steak': 5, 'water': 9, 'cheese': 4, 'spam': 2, 'pizza': 8}
groceries = ['banana', 'steak', 'cheese', 'pizza']
groceries.sort(key = lambda x: locations[x])
the difference is, you can't really find banana in your structure easily
You don't have a dictionary, rather a tuple containing one list with tuples for elements
complicated to access, and not efficient.
with the dictionary sparkz_alot recommensd, access is simple
# shopping list:
glist = {'banana':1, 'steak':5, 'water':9, 'cheese':4} # Note the curly braces
shop_list = ['water', 'cheese', 'steak']
for food in shop_list:
print('{} is located in isle: {}'.format(food, glist[food]))
Output:
water is located in isle 9
cheese is located in isle 4
steak is located in isle 5
Thak you all! This helped me already quite a lot.
The code looks now like this:
locations = {'banana': 1, 'steak': 5, 'water': 9, 'cheese': 4, 'spam': 2, 'pizza': 8}
groceries = ['banana', 'steak', 'spam', 'cheese', 'pizza']
groceries.sort(key = lambda x: locations[x])
for item in groceries:
print('{} is located in isle: {}'.format(item, locations[item]))
Output:
banana is located in isle: 1
spam is located in isle: 2
cheese is located in isle: 4
steak is located in isle: 5
pizza is located in isle: 8
in the output are the items ordered after their values as stated in the dictonairy, that´s perfect.
Next step would be to somehow let python accept for groceries an input for groceries, like
locations = {'banana': 1, 'steak': 5, 'water': 9, 'cheese': 4, 'spam': 2, 'pizza': 8}
groceries = input ('What would you like to buy? ')
then you enter your list and it returns the entered items arranged from isle 1-10.