python学习-17 列表list 2
2024-09-01 14:34:04
# 1. 选择嵌套列表里的元素(内部进行了for循环) li = [1,2,"age",["熊红","你好",["",45]],"abc",True]
a = li[3][2][1]
print(a)
运行结果:
45 Process finished with exit code 0
#2. 字符串转换列表
a = "iamchinese"
b =list(a)
print(b)
运行结果:
['i', 'a', 'm', 'c', 'h', 'i', 'n', 'e', 's', 'e'] Process finished with exit code 0
#3. 列表转换字符串
第一种方法:(for循环)
li =[123,456,"abcdefg"]
b = " "
for a in li:
b=b+str(a)
print(b)
运算结果:
123456abcdefg Process finished with exit code 0
第二种方法:(列表中的元素只有字符串时)
li =["abcdefg"]
a ="".join(li)
print(a)
运行结果:
abcdefg Process finished with exit code 0
#4. list 的其他功能
-追加(也可以加字符串,列表等)
li = [123,13121,55]
li.append(6)
print(li)
运算结果:
[123, 13121, 55, 6] Process finished with exit code 0
-清空
li = [123,13121,55]
li.clear()
print(li)
运算结果:
[] Process finished with exit code 0
-浅拷贝
li = [123,13121,55]
a = li.copy()
print(li)
print(a)
运算结果:
[123, 13121, 55]
[123, 13121, 55] Process finished with exit code 0
-计数
li = [55,123,13121,55]
a =li.count(55) #计算 55 出现过几次 print(a)
运算结果:
2 Process finished with exit code 0
-迭加
li = [1,2,3,4,5]
li.extend([7,8,9,"你好"]) print(li)
运算结果:
[1, 2, 3, 4, 5, 7, 8, 9, '你好'] Process finished with exit code 0
ps: append 将整个元素添加, extend 是内部经过for循环将一个添加
-获取位置
li = [1,22,33,4,5,33]
a=li.index(33) #(从左向右获取索引位置(获取到第一个就不会继续了),可以指定位置li.index(0:3)) print(a)
运算结果:
2 #(索引,不是数量) Process finished with exit code 0
-删除某个元素并获取删除的元素
li = [1,22,33,4,5,33]
a=li.pop() # 可以指定索引位置(不指定索引,默认删除最后一个)
print(li)
print(a)
运算结果:
[1, 22, 33, 4, 5]
33 Process finished with exit code 0
-删除指定元素
li = [1,22,33,4,5,33]
li.remove(33) #从左向右只删除第一个
print(li)
运算结果:
[1, 22, 4, 5, 33] Process finished with exit code 0
-反转
li = [1,22,33,4,5,33]
li.reverse()
print(li)
运算结果:
[33, 5, 4, 33, 22, 1] Process finished with exit code 0
-排序
li = [1,22,33,4,5,33]
li.sort(reverse=True) # 括号里不填默认从小到大排序
print(li)
运算结果:
[33, 33, 22, 5, 4, 1] Process finished with exit code 0
最新文章
- 60.Android通用流行框架大全
- SQLite手工注入方法小结
- (二) 一起学 Unix 环境高级编程 (APUE) 之 文件 IO
- typeof和instanceof简介及用法
- 解决:打开OleView报错 dllregisterserver in iviewers failed
- Informix如何释放异常的锁资源
- 【Windows】如何判断当前鼠标是否按下左键或右键
- Ombrophobic Bovines
- Android 自定义view实现水波纹效果
- 2014 ACM/ICPC Asia Regional Beijing Site
- 1-安装MQTT服务器(Windows)
- AVR 嵌入式单片机芯片的中断系统介绍
- MYSQL事务处理失效原因
- linux 进程 ctrl-c,ctrl-z,ctrl-d
- python 全栈开发,Day25(复习,序列化模块json,pickle,shelve,hashlib模块)
- Jenkins常见任务配置
- Android Timer的应用示例
- IE8崩溃在CElement::GetUpdatedLayoutWithContext
- Activity 在横竖屏切换情况下的生命周期变化
- jquery不能是使用普通的for循环 因为普通的for循环通过下表获取对象 如果通过下表获取对象的话 会转成dom对象
热门文章
- D-【乐】k进制数(同余)
- C++ <;Algorithm>;小小总结
- 配置mysql远程访问
- 2018-2019-2 20165312《网络攻防技术》Exp 8 Web基础
- git clone 报“The project you were looking for could not be found.”
- Flutter移动电商实战 --(40)路由_Fluro的全局注入和使用方法
- Git放弃修改
- Docs-.NET-C#-指南-语言参考-预处理器指令:#endif(C# 参考)
- myadmin不需要路劲提权之法
- Vue.js学习之简介(待续)