list中的sort()方法

def sort(self, key=None, reverse=False): # real signature unknown; restored from doc
“”" L.sort(key=None, reverse=False) -> None – stable sort IN PLACE “”"
pass
‘’’
key:是排序的条件,可以是:key=int,key=len, key=lambda…
reverse:表示是否反序,默认从小到大,默认为Flase

##一个list调用sort方法后,对原list进行排序
##1、最简单的排序
l = [5,2,3,1,4 ]
l .sort()
print(l) ##输出:[1, 2, 3, 4, 5]

l.sort(reverse=True)#反序
print(l) ##输出:[5, 4, 3, 2, 1]

##2、字符串排序
StrList = [‘fb’, ‘bx’, ‘csw’, ‘qb’, ‘qqa’, ‘eeeed’]
2.1 一般字典序排列,但是大写在前,小写在后!!
StrList.sort()
print(StrList) ##字符串列表是按照第一个字符的大小排序的
##输出:[‘Fast’, ‘Smooth’, ‘fast’, ‘is’, ‘is’, ‘smooth’]

2.2忽略大小写,按abcd顺序
StrList.sort(key=str.lower)
print(StrList) ##输出:[‘Fast’, ‘fast’, ‘is’, ‘is’, ‘Smooth’, ‘smooth’]

2.3按照字符串长度排序
StrList.sort(key=len)
print(StrList)##输出:[‘is’, ‘is’, ‘fast’, ‘Fast’, ‘Smooth’, ‘smooth’]

StrList.sort(key=len, reverse=True)#反序
print(StrList) ##输出:[‘Smooth’, ‘smooth’, ‘fast’, ‘Fast’, ‘is’, ‘is’]

你可能感兴趣的:(嵌入式,python,列表,字符串)