输入搜索字典python

时间:2017-04-23 12:12:31

标签: python sorting dictionary input

我希望python从我这里获取一个输入(一个购物清单),它会返回输入,但会在字典值之后排序。

locations = {'banana': 1, 'steak': 5, 'water': 9, 'cheese': 4, 'wine': 2, 'pizza': 8}
#today I want to buy banana and cheese
groceries = ['banana', 'cheese'] # this should be the input instead
groceries.sort(key = lambda x: locations[x])

for item in groceries:
   print('{} is located in area: {}'.format(item, locations[item]))

输出:

banana位于地区:1

奶酪位于地区:4

我现在想要的是功能'杂货'将是一个输入。我该怎么办呢?非常感谢!!

0 个答案:

没有答案