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
#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


Messages In This Thread
sorting groceries by position in store - by bionoob - Apr-22-2017, 12:21 PM
RE: sorting groceries by position in store - by bionoob - Apr-22-2017, 02:46 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Sorting a copied list is also sorting the original list ? SN_YAZER 3 3,158 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