Python 练习实例90

[复制链接]
407|0
 楼主| cooldog123pp 发表于 2023-5-27 12:35 | 显示全部楼层 |阅读模式
题目:列表使用实例。
程序分析:无。
  1. #!/usr/bin/python
  2. # -*- coding: UTF-8 -*-

  3. #list  
  4. #新建列表  
  5. testList=[10086,'中国移动',[1,2,4,5]]  
  6.   
  7. #访问列表长度  
  8. print len(testList)  
  9. #到列表结尾  
  10. print testList[1:]  
  11. #向列表添加元素  
  12. testList.append('i\'m new here!')  
  13.   
  14. print len(testList)  
  15. print testList[-1]  
  16. #弹出列表的最后一个元素  
  17. print testList.pop(1)  
  18. print len(testList)  
  19. print testList  
  20. #list comprehension  
  21. #后面有介绍,暂时掠过  
  22. matrix = [[1, 2, 3],  
  23. [4, 5, 6],  
  24. [7, 8, 9]]  
  25. print matrix  
  26. print matrix[1]  
  27. col2 = [row[1] for row in matrix]#get a  column from a matrix  
  28. print col2  
  29. col2even = [row[1] for row in matrix if  row[1] % 2 == 0]#filter odd item  
  30. print col2even
以上实例输出结果为:

  1. ['\xe4\xb8\xad\xe5\x9b\xbd\xe7\xa7\xbb\xe5\x8a\xa8', [1, 2, 4, 5]]
  2. 4
  3. i'm new here!
  4. 中国移动
  5. 3
  6. [10086, [1, 2, 4, 5], "i'm new here!"]
  7. [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
  8. [4, 5, 6]
  9. [2, 5, 8]
  10. [2, 8]


您需要登录后才可以回帖 登录 | 注册

本版积分规则

2304

主题

7628

帖子

31

粉丝
快速回复 在线客服 返回列表 返回顶部