python中sort()
时间: 2024-05-16 16:19:05 浏览: 92
Sort()方法是Python中用于对列表进行排序的方法。它是应用在列表上的方法,会在原位重新排列列表,无返回值。而sorted()函数是对所有可迭代的对象进行排序操作,并返回一个新的列表。
下面是一些sort()方法的用法举例:
1. 按照ASCII升序排序:
```python
aList = ['123', 'Google', 'Runoob', 'Taobao', 'Facebook']
aList.sort()
print(aList)
# 输出 ['123', 'Facebook', 'Google', 'Runoob', 'Taobao']
```
2. 降序输出列表:
```python
vowels = ['e', 'a', 'u', 'o', 'i']
vowels.sort(reverse=True)
print('降序输出:')
print(vowels)
# 输出 ['u', 'o', 'i', 'e', 'a']
```
3. 按照函数排序:
```python
# 获取列表的第二个元素
def takeSecond(elem):
return elem<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [python的sort()函数用法详解](https://blog.csdn.net/qq_35759272/article/details/124564586)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *3* [python 的sort()函数详解](https://blog.csdn.net/qq_20831401/article/details/121663509)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文