[Python] python里的列表list使用范例代码 →→→→→进入此内容的聊天室

来自 , 2021-02-12, 写在 Python, 查看 109 次.
URL http://www.code666.cn/view/26c0a195
  1. #list
  2. #新建列表
  3. testList=[10086,'中国移动',[1,2,4,5]]
  4.  
  5. #访问列表长度
  6. print len(testList)
  7. #到列表结尾
  8. print testList[1:]
  9. #向列表添加元素
  10. testList.append('i\'m new here!')
  11.  
  12. print len(testList)
  13. print testList[-1]
  14. #弹出列表的最后一个元素
  15. print testList.pop(1)
  16. print len(testList)
  17. print testList
  18. #list comprehension
  19. #后面有介绍,暂时掠过
  20. matrix = [[1, 2, 3],
  21. [4, 5, 6],
  22. [7, 8, 9]]
  23. print matrix
  24. print matrix[1]
  25. col2 = [row[1] for row in matrix]#get a  column from a matrix
  26. print col2
  27. col2even = [row[1] for row in matrix if  row[1] % 2 == 0]#filter odd item
  28. print col2even
  29. #//python/4452

回复 "python里的列表list使用范例代码"

这儿你可以回复上面这条便签

captcha