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