Python语言基础与应用 (P23)上机练习:容器类型操作(未完待续)
2024-09-27 22:17:23
上机练习:容器类型操作
〉 列表、元组基本操作
+, *, len(), [], in
Python 3.7.0 (default, Jun 28 2018, 08:04:48) [MSC v.1912 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> t1=tuple(range(1,10,2))
>>> t1
(1, 3, 5, 7, 9)
>>> t2=tuple(range(2,11,2))
>>> t2
(2, 4, 6, 8, 10)
>>> t1+t2
(1, 3, 5, 7, 9, 2, 4, 6, 8, 10)
>>> len(t1)
5
>>> t2[3]
8
>>> 9 in t1
True
>>> 7 in t2
False
>>> L1=list(range(15))
>>> L1
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
>>> L2=L1*3
>>> L2
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
>>> 99 in L2
False
>>> len(L2)
45
>>> list(t2)+L1
[2, 4, 6, 8, 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
〉 列表、元组高级操作
mylist=[1,2,3,4,5]
切片:获得[2,3,4],获得[3,4,5],获得[3,2,1],
获得[1,3,5]
mytpl=(1,2,3,4,5)同上操作
t='Mike and Tom'
split拆分、 join合成为'Mike/and/Tom
>>> mylist=[1,2,3,4,5]
>>> list1=mylist[1:4]
>>> list1
[2, 3, 4]
>>> list2=mylist[2:]
>>> list2
[3, 4, 5]
>>> list3=mylist[2:0:-1]
>>> list3
[3, 2]
>>> list3=mylist[2::-1]
>>> list3
[3, 2, 1]
>>> list4=mylist[-3::-1]
>>> list4
[3, 2, 1]
>>> list5=mylist[::2]
>>> list5
[1, 3, 5]
>>> mytpl=tuple(mylist)
>>> mytpl
(1, 2, 3, 4, 5)
>>> tu1=mytpl[1:4]
>>> tu1
(2, 3, 4)
>>> tu2=mytpl[2::-1]
>>> tu2
(3, 2, 1)
>>> tu3=mytpl[::2]
>>> tu3
(1, 3, 5)
>>> t='Mike and Tom'
>>> t_new=t.split(' ').join("/")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'join'
>>> t_new=t.split(' ')
>>> t_new
['Mike', 'and', 'Tom']
>>> t_n=str(t_new)
>>> t_n
"['Mike', 'and', 'Tom']"
>>> t_n=t.replace(" ","/")
>>> t_n
'Mike/and/Tom'
总结:split方法中如果不传参,默认以空格分割。list没有join方法,只有str才有。join()中的参数是个序列,可以是list,tuple,set,但最好不要用set,因为set中的元素是无序的。
最新文章
- java javacv调用摄像头并拍照
- dockerfile学习与详解
- Windows phone 8 学习笔记(4) 应用的启动(转)
- next permutaion算法
- 南阳理工ACM975--关于521
- cocoapods 终极方案
- python staticmethod classmethod
- Linux 脚本整理
- Git操作流水账
- python--ftp服务器(pyftpdlib)
- Linux增加开放端口号
- ubuntu命令安装
- RNQOJ PID28 / [Stupid]愚蠢的宠物
- [20180814]慎用查看表压缩率脚本.txt
- 【转】推荐4个不错的Python自动化测试框架
- js 异步加载
- Python——字符串(python programming)
- [Android] android.util.Log
- mysql查询字段为null 返回0
- android bluetooth
热门文章
- UVA 1601 双向BFS
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):带有字体图标的导航栏
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-file
- Spring入门之五-------SpringIoC之通过注解实现
- linux下安装redis,按照redis官网安装不成功需要提前安装c++环境(安装成功并可以测试)
- python itertools 用法
- python爬取网页文本、图片
- linux X64函数参数传递过程研究
- c++程序—布尔值
- SpringBoot的Banner横幅