最近看到一道面试题很有意思:
用一个lambda函数实现排序,排序后正数在前(由小到大),负数在后(由大到小),如列表[7,-8,5,4,0,-2,-5],排序后变为[0, 4, 5, 7, -2, -5, -8]
答案如下,有待优化:
list = [7,-8,5,4,0,-2,-5] list.sort(key=lambda x:x*-100 if x<0 else x ) print(list)
时间: 2024-10-08 04:28:23
最近看到一道面试题很有意思:
用一个lambda函数实现排序,排序后正数在前(由小到大),负数在后(由大到小),如列表[7,-8,5,4,0,-2,-5],排序后变为[0, 4, 5, 7, -2, -5, -8]
答案如下,有待优化:
list = [7,-8,5,4,0,-2,-5] list.sort(key=lambda x:x*-100 if x<0 else x ) print(list)