Python Forum
sorting groceries by position in store
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
sorting groceries by position in store
#1
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
Reply
#2
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
Reply
#3
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.
Reply
#4
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])
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
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
Reply
#6
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sorting a copied list is also sorting the original list ? SN_YAZER 3 3,109 Apr-11-2019, 05:10 PM
Last Post: SN_YAZER

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020