python中sort用法

 时间:2024-10-13 07:20:47

1、sort是用来排序列表的。a=[3,1,2]a.sort()print(a)给出列表a的元素排序,默认的是从小到大排列。

python中sort用法

2、a.sort(reverse=True)则是反向排序,从大到小排列。

python中sort用法

3、字母之间也存在先后顺序:a=['a','c','b']a.sort()

python中sort用法

4、大写字母排在小写字母前面:a=['a','A','b','B','c','C']

python中sort用法

5、数亨蚂擤缚字变成的字符串,排在大写字母前面:a=['1','12','123&#泌驾台佐39;, 'a','A','b', 'B','c','C']

python中sort用法

6、英文的标点符号,排在数字型字符串前面:a=[',','.','aBC', '1',&#泌驾台佐39;12','123', 'a','A','b', 'B','c','C']

python中sort用法

7、中文的标点符号,排在英文字母后面,而且句号排在逗号前面:a=[',','.',',','。', '1','12','123', 'a','A','b', 'B','c','C']

python中sort用法

8、列表里面如果即有数字(非字符串形式的数字),又有字符串,则无法排序。原因是:'<' not supported between instances of 'int' and 'str';无法比较数字和字符串的大小。

python中sort用法
  • python输出怎么换行
  • python创建列表
  • python删除重复元素
  • python 字符串转列表
  • python每行输出5个数据
  • 热门搜索
    脖子上长小肉疙瘩怎么办 dnf红字怎么打 手机开不开机怎么办 奥尔良烤翅怎么做 身上痒怎么办 怎么设置鼠标灵敏度 炒面怎么做好吃 我的世界怎么调中文 gorilla怎么读 流鼻涕怎么办