Python,列表
来源:互联网 发布:化工公司工艺软件 编辑:程序博客网 时间:2024/06/05 15:13
1.list() 函数:--适用于所有类型的序列
print(list("Wang"))["W","a","n","g"]
2.修改列表元素
number=[1,2,3]number[1]=4print(number)[1,4,3]
3.删除元素
name=["wang","zhong"]del name[1]print(name)["wang"]
4.分片赋值
name=list("wamf")name[2:]=list("ng")print(name)["w","a","n","g"]
number=[1,2,3]number[0:2]=[7,8,9]print(number)[7,8,9,3]
5.列表方法
append()--列表末尾增加新对象
number=[1,2,3]number.append(4)print(number)[1,2,3,4]
count()--统计某个元素出现的次数
num=[[1,2],2,2,[2,3]]print(num.count(2))2
extend()--扩展原有序列,原地操作
a=[1,2,3]b=[4,5,6]a.extend(b)print(a)[1,2,3,4,5,6]
index()--查找某个元素在列表中的第一个位置
num=[1,2,3,4,5,6]print(num.index(3))2
insert()--插入元素到指定位置
num[1,2,3,4,5,6]num.insert(3,0)print(num)[1,2,3,0,4,5,6]
pop()--移除列表中某个位置的元素,默认是最后一个,并返回该元素的值,
num=[1,2,3]num.pop(1)2
remove()--移除列表中指定元素,没有返回值
num=[1,2,3]print(num.remove(1))[1,3]
reverse()--反向存放列表元素,改变列表,没有返回值 || reversed(num)
num=[1,2,3]print(num.reverse())[3,2,1]
sort()--对原来列表排序,改变原列表 || sorted(num)
num=[4,5,6,1,2,3]print(num.sort())[1,2,3,4,5,6]
6.高级排序
compare(x,y) x<y-负 ;x>y-正;x=y-0;
sort()-参数 key reverse
x=["aaa","bb","c"]print(x.sort(key=len))["c","bb","aaa"]
x=[1,5,9,3,4,7]print(x.sort(reverse=True))[9,7,5,4,3,1]
阅读全文
0 0
- Python 列表
- python 列表
- python--列表
- python 列表
- Python 列表
- Python 列表
- python 列表
- Python列表
- python列表
- Python列表
- Python列表
- python列表
- Python列表
- python 列表
- python列表
- python列表
- Python--列表
- Python-列表
- Java中的正则表达式
- Java服务端推送和web端推送——GoEasy免费推送服务
- threadlocal原理及常用应用场景2
- LOJ6002 「网络流 24 题
- react踩坑不完全指北(1)
- Python,列表
- 递归再理解
- sublime text3安装以及插件配置教程
- Java设计模式透析之 —— 模板方法(Template Method)
- layout_below与layout_alignBottom的区别
- poi工具类
- RigidBody的基础知识
- 去除读文件的换行
- 进程