Posts: 3
Threads: 1
Joined: Apr 2017
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
Posts: 1,298
Threads: 38
Joined: Sep 2016
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?
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Posts: 3
Threads: 1
Joined: Apr 2017
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.
Posts: 4,220
Threads: 97
Joined: Sep 2016
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])
Posts: 12,024
Threads: 484
Joined: Sep 2016
Apr-22-2017, 01:57 PM
(This post was last modified: Apr-22-2017, 01:57 PM by Larz60+.)
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
Posts: 3
Threads: 1
Joined: Apr 2017
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.
|