You have a table with all available goods in the store. The data is represented as a list of dicts
Your mission here is to find the TOP most expensive goods. The amount we are looking for will be given as a first argument and
the whole data as the second one
Input: int and list of dicts. Each dicts has two keys "name" and "price"
Output: the same as the second Input argument.
Example:
bigger_price(2, [
{"name": "bread", "price": 100},
{"name": "wine", "price": 138},
{"name": "meat", "price": 15},
{"name": "water", "price": 1}
]) == [
{"name": "wine", "price": 138},
{"name": "bread", "price": 100}
]
bigger_price(1, [
{"name": "pen", "price": 5},
{"name": "whiteboard", "price": 170}
]) == [{"name": "whiteboard", "price": 170}]
# demo1
b = ['aa', 'BB', 'bb', 'zz', 'CC']
print(sorted(b)) # 按列表中元素每个字母的ascii码从小到大排序,如果要从大到小,请用sorted(b,reverse=True)下同
# demo2
d = ['CCC', 'bb', 'ffff', 'z']
print(sorted(d, key=str.lower)) # 将列表中的每个元素变为小写,再按每个元素中的每个字母的ascii码从小到大排序
print(sorted(d, key=len)) # 按列表的元素的长度排序
#demo3
# 自定义函数排序,lastchar为函数名,这个函数返回列表e中每个元素的最后一个字母
def lastchar(s):
return s[-1]
e = ['abc', 'b', 'AAz', 'ef']
print(sorted(e, key=lastchar)) # sorted(e,key=lastchar)作用就是 按列表e中每个元素的最后一个字母的ascii码从小到大排序
print(sorted(e, key=lambda x: x[-1])) # 如果觉得上面定义一个函数代码不美观,可以用lambda的形式来定义函数,效果同上