checkio练习题:bigger-price

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的形式来定义函数,效果同上 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值