Python教程

012-Python入门(列表及其常用的方法2)

本文主要是介绍012-Python入门(列表及其常用的方法2),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
1.clear():清空列表
name_List = ['jch', '小明', '小红', '小王']
name_List.clear()
print(name_List)            #[]
2.逆置:reverse()
name_List = ['jch', '小明', '小红', '小王']
name_List.reverse()
print(name_List)         #['小王', '小红', '小明', 'jch']
3.排序:sort()--语法:列表序列.sort( key=None, reverse=False)
# 注意:reverse表示排序规则,reverse = True 降序, reverse = False 升序(默认)
name_List1 = [1, 25, 6, 9, 46]
name_List1.sort()
print(name_List1)       #[1, 6, 9, 25, 46]
name_List1.sort(reverse=True)
print(name_List1)       #[46, 25, 9, 6, 1]
4. 复制--函数:copy()
name_List = ['jch', '小明', '小红', '小王']
name_List1 = [1, 25, 6, 9, 46]
name_List = name_List1.copy()
print(name_List)        #[1, 25, 6, 9, 46]
这篇关于012-Python入门(列表及其常用的方法2)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!