python列表排序

代码如下:

def getprice(item):
    # 切分,获取名字与价格
    name,price=item.split(":")
    # 字符串转整数,并返回
    return int(price)
phoneprice = '魅族X8:1798,红米6:849,荣耀8X:1399,小米8:2499,小米8SE:2099,荣耀畅玩7A:699'
# 字符串切分
items=phoneprice.split(",")
print(items)
# 按照价格从小到大排序
items.sort(key=getprice)
print(items)

执行结果:
python列表排序

猜你喜欢

转载自blog.51cto.com/13866762/2321208
今日推荐