1.list的定义方法
arr=["abc","def","ghi"]
2.append方法--向列表尾部追加元素
arr=["abc","def","ghi"] arr.append("jkl") print(arr);
结果为:[‘abc‘, ‘def‘, ‘ghi‘, ‘jkl‘]
3.clear方法-清除列表中的所有数据
arr=["abc","def","ghi"] arr.clear() print(arr);
结果为: []
4.copy方法-复制list中的所有元素给其他list
arr=["abc","def","ghi"] arr2=arr.copy() print(arr2);
结果为:[‘abc‘, ‘def‘, ‘ghi‘]
5.count("")方法--统计指定元素在list中出现的次数
arr=["abc","def","abc"] count=arr.count("abc") print(count);
结果为:2
6.extend()将某个list的所有元素向指定list尾部追加
arr=["abc","def","abc"] list2=["123","456","789"] arr.extend(list2) print(arr);
结果为:[‘abc‘, ‘def‘, ‘abc‘, ‘123‘, ‘456‘, ‘789‘]
7.index()--得到list中指定元素第一次出现的位置
arr=["abc","def","abc"] index=arr.index("abc") print(index);
结果为:0
8.insert()--向list指定下标指定位置插入元素
arr=["abc","def","abc"] arr.insert(0,"你好") print(arr);
结果为:[‘你好‘, ‘abc‘, ‘def‘, ‘abc‘]
9.pop()移除指定list的最后一个元素
arr=["abc","def","abc"] arr.pop(); print(arr);
结果为:[‘abc‘, ‘def‘]
10.remove()--移除指定元素在list中从左至右第一次出现的元素
arr=["abc","def","abc"] arr.remove("abc"); print(arr);
结果为:[‘def‘, ‘abc‘]
11.reverse()--将指定list翻转顺序
arr=["1","2","3"] arr.reverse(); print(arr);
结果为:[‘3‘, ‘2‘, ‘1‘]
12.sort()--对指定list进行排序(如果list同时出现字母和数字,则会抛异常)
arr=["3","1","2","4","9","7","8","5","0"] arr.sort(); print(arr);
结果为:[‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘7‘, ‘8‘, ‘9‘]
13.del关键字:可以删除list指定下标范围的元素
arr=["a","b","c","d","e","f","g"] del arr[0:3] print(arr);
结果为:[‘d‘, ‘e‘, ‘f‘, ‘g‘]
--------------------------------------结束------------------------------------------------------------
第二天学习python,精力还是很充沛,希望继续怒力,这样就离Python大神不远了!
继续学习去了,不吹了
时间: 2024-11-13 06:59:39