-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlist_practice
More file actions
51 lines (42 loc) · 999 Bytes
/
list_practice
File metadata and controls
51 lines (42 loc) · 999 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""
关于列表的基本操作
"""
list1 = ["中国", "美国", 1997, 2001]
list2 = ["中国", "美国", 1997, 2001]
#1.删除元素的三种方法
del list1[2]
print("After deleting a value:")
print(list1)
list1 = list2
list1.remove(1997)
print("After deleting a value:")
print(list1)
list1 = list2
list1.pop(2)
list1.pop() #默认删除最后一个元素
print("After deleting two values:")
print(list1)
#2.添加列表元素
list1.append(2003)
print("After appending an element:")
print(list1)
#3.定义多维列表
rows = 3
cols=4
matrix = [[0 for col in range(cols)] for row in range(rows)]
for i in range(rows):
for j in range(cols):
matrix[i][j] = i* 3 + j
print(matrix[i][j], end = ',')
print('\n')
"""
输出结果:
0,1,2,3,
3,4,5,6,
6,7,8,9,
"""
#4.输出10以内偶数的平方列表
list3 = [x*x for x in range(1, 11) if x*x%2 == 0]
#5.列表生成式使用两层循环
print([m+n for m in 'ABC' for n in 'XYZ'])
#6.for循环可以同时使用两个甚至多个变量