[python3]: 50个【空元素】初始化数据结构【list】
一、需求
1、 需求:用50个空元素,初始化一个数据结构‘列表’。 lst = [ '' for i in range(50) ]
2、 需求: 改变某个列表元素的值。 lst = [ '' for i in range(50) ] ; lst[variable] = variable ;
二、代码:
1 #!/usr/bin/env python3
2
3
4 # set 50 empty elements for a list
5 ls = [ '' for i in range(50) ]
6
7
8 # change the first element value of a list to 100
9 ls[0] = 100
10
11
12 # name: trace(ls):
13 # function: display elements of a list
14 # parameters: list
15 # return: none
16 def trace(ls):
17 id = 0
18 for e in ls:
19 print(f"[index_{id}]\t:=\t{e}")
20 id = id + 1
21
22
23 # entry point
24 if __name__ == '__main__' :
25 trace(ls)
三、运行结果:
1 [index_0] := 100
2 [index_1] :=
3 [index_2] :=
4 [index_3] :=
5 [index_4] :=
6 [index_5] :=
7 [index_6] :=
8 [index_7] :=
9 [index_8] :=
10 [index_9] :=
11 [index_10] :=
12 [index_11] :=
13 [index_12] :=
14 [index_13] :=
15 [index_14] :=
16 [index_15] :=
17 [index_16] :=
18 [index_17] :=
19 [index_18] :=
20 [index_19] :=
21 [index_20] :=
22 [index_21] :=
23 [index_22] :=
24 [index_23] :=
25 [index_24] :=
26 [index_25] :=
27 [index_26] :=
28 [index_27] :=
29 [index_28] :=
30 [index_29] :=
31 [index_30] :=
32 [index_31] :=
33 [index_32] :=
34 [index_33] :=
35 [index_34] :=
36 [index_35] :=
37 [index_36] :=
38 [index_37] :=
39 [index_38] :=
40 [index_39] :=
41 [index_40] :=
42 [index_41] :=
43 [index_42] :=
44 [index_43] :=
45 [index_44] :=
46 [index_45] :=
47 [index_46] :=
48 [index_47] :=
49 [index_48] :=
50 [index_49] :=
四、参考资料:
1、 Python3 列表|菜鸟教程 -- https://www.runoob.com/python/python-lists.html
本文由 lnlidawei 原创、整理、转载,本文来自于【博客园】; 整理和转载的文章的版权归属于【原创作者】; 转载或引用时请【保留文章的来源信息】:https://www.cnblogs.com/lnlidawei/p/18009077