1. 程式人生 > >python列表排序

python列表排序

turn sha term hone 字符串 text 價格 watermark 字符串轉整數

代碼如下:

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列表排序