Python基础Lists和tuple实例详解
作者:王者之峰 时间:2021-09-27 04:11:36
Lists
列表可以包含不同类型的元素,甚至是Lists,但是通常是同一个类型的。
if __name__ == '__main__':
squares = [1, 4, [1, 2], "whf", 25]
print(squares)
索引和切片
列表支持使用下标索引元素,支持切片.
if __name__ == '__main__':
squares = [1, 4, [1, 2], "whf", 25]
item1 = squares[0]
print(item1)
item2 = squares[-1]
print(item2)
squaresShallowCopy = squares[1:3]
print(squaresShallowCopy)
print(squaresShallowCopy[0])
squaresShallowCopy[1:2]=[]
print(squaresShallowCopy)
输出:
1
25
[4, [1, 2]]
4
[4]
所有切片操作都会返回一个包含请求元素的新列表,被称为原列表的浅副本。
增删改
增
if __name__ == '__main__':
squares = [1, 4, [1, 2], "whf", 25]
squares.insert(1,3)
print(squares)
输出:
[1, 3, 4, [1, 2], 'whf', 25]
删除
pop无参数就弹出尾部的,有参数可以指定位置:
if __name__ == '__main__':
squares = [1, 4, [1, 2], "whf", 25]
squares.pop(1)
print(squares)
输出:
[1, [1, 2], 'whf', 25]
改
if __name__ == '__main__':
squares = [1, 4, [1, 2], "whf", 25]
squares[0]=0
print()
输出:
[0, 4, [1, 2], 'whf', 25]
连接/拼接
if __name__ == '__main__':
squares = [1, 4, [1, 2], "whf", 25]
squares+=[66,77]
print(squares)
squares.append("88")
print(squares)
print(len(squares))
输出:
[1, 4, [1, 2], 'whf', 25, 66, 77]
[1, 4, [1, 2], 'whf', 25, 66, 77, '88']
8
tuple
tuple和list比较类似,但是tuple是不可变的,所以不能增删改。
tuple使用括号括起来,使用逗号分隔元素,如果是简单的元组可以不用:
t = 1, 2, 3
print(t)
t = ((1, 2, 3), (4, 5, 6))
print(t)
empty = ()
print(empty)
singleton = 'hello',
print(singleton)
print(len(singleton))
输出:
((1, 2, 3), (4, 5, 6))
()
('hello',)
1
解包
t = 1, 2, 3
x, y, z = t
print(x, y, z)
输出:
1 2 3
元素是可变的仍然可变
这个优点像java的final,不能变引用,内容你想变还是可以的:
if __name__ == '__main__':
t = ((1, 2, 3), [4, 5, 6])
list=t[1]
list[0]=3
print(t)
输出:
((1, 2, 3), [3, 5, 6])
namedtuple
具名元组,顾名思义就是让普通元组具有名字,方便对元素进行命名和访问:
Student = namedtuple('Student', ['name', 'age', 'city'])
s = Student('Xiaoming', '19', 'Beijing')
print(s)
print(s[1])
print(getattr(s, 'city'))
输出:
Student(name='Xiaoming', age='19', city='Beijing')
19
Beijing
来源:https://juejin.cn/post/7135421305284722702
标签:Python,Lists,tuple
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
跨平台、多浏览器页面测试
2008-06-24 11:54:00
![](https://img.aspxhome.com/file/UploadPic/20086/24/2008624115920735s.gif)
Python类的继承、多态及获取对象信息操作详解
2023-09-09 21:27:52
Go语言实现AOI区域视野管理流程详解
2023-06-20 03:21:51
![](https://img.aspxhome.com/file/2023/7/90477_0s.png)
python正则表达式的使用
2023-07-21 07:42:15
怎么让按钮更容易被点击
2007-08-22 09:07:00
![](https://img.aspxhome.com/file/UploadPic/20078/22/200782291027419s.gif)
Keras 使用 Lambda层详解
2021-08-11 20:12:40
![](https://img.aspxhome.com/file/2023/0/105710_0s.jpg)
Python+Turtle动态绘制一棵树实例分享
2021-10-03 00:33:20
![](https://img.aspxhome.com/file/2023/6/76866_0s.gif)
前端页面文件拖拽上传模块js代码示例
2023-09-20 02:15:30
![](https://img.aspxhome.com/file/2023/1/56151_0s.jpg)
基于python实现音乐播放器代码实例
2022-07-17 21:55:53
浅析Python中的随机采样和概率分布
2023-12-11 14:51:11
在VScode里面添加Python解释器的详细步骤
2021-12-04 16:03:11
![](https://img.aspxhome.com/file/2023/7/100817_0s.png)
Python定时执行之Timer用法示例
2021-09-14 21:46:01
Python中常用操作字符串的函数与方法总结
2023-07-25 12:09:23
三分钟学会Sql Server的复制功能[图]
2008-07-18 12:18:00
![](https://img.aspxhome.com/file/UploadPic/20087/18/2008718122618221s.gif)
MySQL安全性指南 (2)
2010-07-26 13:26:00
pytorch中的embedding词向量的使用方法
2022-03-25 09:05:27
pytorch 如何使用amp进行混合精度训练
2023-12-25 13:35:02
![](https://img.aspxhome.com/file/2023/0/88960_0s.jpg)
深入理解NumPy简明教程---数组2
2022-03-24 05:01:18
GIt在pyCharm的详细使用教程记录
2021-11-21 02:21:05
![](https://img.aspxhome.com/file/2023/3/87893_0s.jpg)
php开发微信支付获取用户地址
2023-09-07 15:12:08