python3: dlt - 数据结构2
一、源程序
1 [wit@fedora null]$ cat test.py
2 #!/usr/bin/env python3
3
4
5
6
7 # file_name = test.py
8 # python_verion = 3.11.1
9
10
11
12
13 # test this script
14 def msg():
15 print("\nhello, python3!\n")
16
17
18 # running msg()
19 # msg()
20
21
22
23
24 # define class for data
25 class dlt_data:
26 def __init__(self):
27 self.no = 0
28 self.id = []
29 self.f = []
30 self.b = []
31
32 def set(self, uno, uid, ufront, uback):
33 self.no = uno
34 self.id = uid
35 self.f.extend(ufront)
36 self.b.extend(uback)
37
38 def title(self):
39 print(f"\n\tNo.\tid\t\tfront\t\t\t\t\t-\tback\t\n")
40
41 def empty_line(sef):
42 print(f"\n")
43
44 def say(self):
45 print(f"\t{self.no}\t{self.id}\t\t{self.f[0]}\t{self.f[1]}\t{self.f[2]}\t{self.f[3]}\t{self.f[4]}\t-\t{self.b[0]}\t{self.b[1]}")
46
47
48
49
50 """
51 dlt = dlt_data()
52 dlt.set(1, 2023001, [18, 19, 24, 27, 34], [11, 12])
53 dlt.title()
54 dlt.say()
55 """
56
57
58
59
60 # display result for testing
61
62
63 """
64 dlt = []
65
66
67 for i in [0,1,2,3,4]:
68 dlt.append(dlt_data())
69 i = 0
70
71
72 c = 0
73 for d in dlt:
74 d.set(c, 2023001+c, [1,12,23,34,35], [1,4])
75 c = c + 1
76 c = 0
77
78
79 dlt[0].title()
80 for x in dlt:
81 x.say()
82 dlt[0].empty_line()
83 """
84
85
86
87
88 # define funtion
89 def speak():
90
91
92 # dlt[][]; dlt = [[list],[list], ...]
93 dlt = []
94
95
96 for i in [0,1,2,3,4]:
97 dlt.append(dlt_data())
98 i = 0
99
100
101 c = 0
102 for d in dlt:
103 d.set(c, 2023001+c, [1,12,23,34,35], [1,4])
104 c = c + 1
105 c = 0
106
107
108 dlt[0].title()
109 for x in dlt:
110 x.say()
111 dlt[0].empty_line()
112
113
114
115
116 # define funtion
117 def speak2():
118
119
120 # dlt[][]; dlt = [[list],[list], ...]
121 dlt = []
122
123 scope = 30
124 for i in range(scope):
125 x = dlt_data()
126 x.set(i, 2022001+i, [2, 4, 7, 14, 31], [3, 9])
127 dlt.append(x)
128 i = 0
129
130
131
132 dlt[0].title()
133 for x in dlt:
134 x.say()
135 x = []
136 dlt[0].empty_line()
137
138
139
140
141 # define function
142 def speak3():
143
144
145 # dlt[][]; dlt = [[list],[list], ...]
146 dlt = []
147
148 scope = 5
149 for i in range(scope):
150 x = dlt_data()
151 uf = [ m+i for m in [1,7,8,12,31]]
152 ub = [ n+i for n in [1,8]]
153 x.set(i, 2022001+i, uf, ub)
154 dlt.append(x)
155 i = 0
156
157
158 """
159 dlt[0].title()
160 for x in dlt:
161 x.say()
162 x = []
163 dlt[0].empty_line()
164 """
165
166
167 # dlt[][]; dlt = [[list],[list], ...]
168 return dlt
169
170
171
172
173 # define output
174 # list_dlt[][]; list_dlt = [[list],[list], ...]
175 def output(list_dlt):
176 print(f"\n-- output: start --")
177 for l in list_dlt:
178 print(f"{l.no}\t{l.id}\t{l.f[0]}\t{l.f[1]}\t{l.f[2]}\t{l.f[3]}\t{l.f[4]}\t-\t{l.b[0]}\t{l.b[1]}")
179 print(f"-- output: end --\n")
180
181
182
183
184 # define output2
185 # list_dlt[][]; list_dlt = [[list],[list], ...]
186 def output2(pl):
187
188
189 s1 = " "
190 s2 = " "
191
192 print(f"\n-- output: start --")
193 for x in pl:
194
195 f1 = x.f[0] - 1
196 f2 = x.f[1] - x.f[0]
197 f3 = x.f[2] - x.f[1]
198 f4 = x.f[3] - x.f[2]
199 f5 = x.f[4] - x.f[3]
200 f6 = 35 - x.f[4]
201
202 sp = " - "
203
204 b1 = x.b[0] - 1
205 b2 = x.b[1] - x.b[0]
206 b3 = 12 - x.b[1]
207
208
209 if x.f[0] < 10 :
210 sf1 = s2*f1 + s1
211 else:
212 sf1 = s2*f1
213
214 if x.f[1] < 10 :
215 sf2 = s2*f2 + s1
216 else:
217 sf2 = s2*f2
218
219 if x.f[2] < 10 :
220 sf3 = s2*f3 + s1
221 else:
222 sf3 = s2*f3
223
224 if x.f[3] < 10 :
225 sf4 = s2*f4 + s1
226 else:
227 sf4 = s2*f4
228
229 if x.f[4] < 10 :
230 sf5 = s2*f5 + s1
231 else:
232 sf5 = s2*f5
233
234 sf6 = s2*f6
235
236 if x.b[0] < 10 :
237 sb1 = s2*b1 + s1
238 else:
239 sb1 = s2*b1
240
241 if x.b[1] < 10 :
242 sb2 = s2*b2 + s1
243 else:
244 sb2 = s2*b2
245
246 sb3 = s2*b3
247
248
249 #print(f"\t{x.no}\t{x.id}\t{sf1}{x.f[0]}{sf2}{x.f[1]}{sf3}{x.f[2]}{sf4}{x.f[3]}{sf5}{x.f[4]}{sf6}{sp}{sb1}{x.b[0]}{sb2}{x.b[1]}{sb3}")
250 print(f"\t{x.no}\t{x.id}\t{sf1}{x.f[0]}{sf2}{x.f[1]}{sf3}{x.f[2]}{sf4}{x.f[3]}{sf5}{x.f[4]}{sf6}{sp}{sb1}{x.b[0]}{sb2}{x.b[1]}{sb3}")
251
252
253 print(f"-- output: end --\n")
254
255
256 """
257 print(f"\n-- output: start --")
258 for l in pl:
259 print(f"\t{l.no}\t{l.id}\t{s*f1}{l.f[0]}{s*f2}{l.f[1]}{s*f3}{l.f[2]}{s*f4}{l.f[3]}{s*f5}{l.f[4]}{s*f6}{sp}{s*b1}{l.b[0]}{s*b2}{l.b[1]}{s*b3}")
260 print(f"-- output: end --\n")
261 """
262
263
264
265
266
267
268
269
270 # test part
271 if __name__ == "__main__":
272 #speak()
273 #speak2()
274 dlt_list = speak3()
275 output2(dlt_list)
276 [wit@fedora null]$
277 [wit@fedora null]$
二、运行结果
1 [wit@fedora null]$ ./test.py
2
3 -- output: start --
4 0 2022001 1 7 8 12 31 - 1 8
5 1 2022002 2 8 9 13 32 - 2 9
6 2 2022003 3 9 10 14 33 - 3 10
7 3 2022004 4 10 11 15 34 - 4 11
8 4 2022005 5 11 12 16 35 - 5 12
9 -- output: end --
10
11 [wit@fedora null]$
12 [wit@fedora null]$
三、参考资料
1、python3: dlt - 数据结构 https://www.cnblogs.com/lnlidawei/p/17825107.html
本文由 lnlidawei 原创、整理、转载,本文来自于【博客园】; 整理和转载的文章版权归属【原创作者】; 转载或引用时【请保留文章的来源信息】:https://www.cnblogs.com/lnlidawei/p/17825486.html

浙公网安备 33010602011771号