上机练习:容器类型操作
〉 列表、元组基本操作
+, *, 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中的元素是无序的。

最新文章

  1. java javacv调用摄像头并拍照
  2. dockerfile学习与详解
  3. Windows phone 8 学习笔记(4) 应用的启动(转)
  4. next permutaion算法
  5. 南阳理工ACM975--关于521
  6. cocoapods 终极方案
  7. python staticmethod classmethod
  8. Linux 脚本整理
  9. Git操作流水账
  10. python--ftp服务器(pyftpdlib)
  11. Linux增加开放端口号
  12. ubuntu命令安装
  13. RNQOJ PID28 / [Stupid]愚蠢的宠物
  14. [20180814]慎用查看表压缩率脚本.txt
  15. 【转】推荐4个不错的Python自动化测试框架
  16. js 异步加载
  17. Python——字符串(python programming)
  18. [Android] android.util.Log
  19. mysql查询字段为null 返回0
  20. android bluetooth

热门文章

  1. UVA 1601 双向BFS
  2. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):带有字体图标的导航栏
  3. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-file
  4. Spring入门之五-------SpringIoC之通过注解实现
  5. linux下安装redis,按照redis官网安装不成功需要提前安装c++环境(安装成功并可以测试)
  6. python itertools 用法
  7. python爬取网页文本、图片
  8. linux X64函数参数传递过程研究
  9. c++程序—布尔值
  10. SpringBoot的Banner横幅