python中对列表进行排序,限定数值型
001、
python中对列表进行排序,限定数值型:
>>> list1 = [2,1,5,10,3,7] ## 测试列表1,数值型,可以直接排序 >>> sorted(list1) [1, 2, 3, 5, 7, 10] >>> list1 = ["2","1","5","10","3","7"] ## 测试列表2,字符型,无法直接按照数值型排序 >>> sorted(list1) ['1', '10', '2', '3', '5', '7'] >>> sorted(list1, key = lambda x: int(x)) ## 利用key参数,将字符型转换为数值型再排序 ['1', '2', '3', '5', '7', '10']
。