python3: dlt - 数据结构2

发布时间 2023-11-11 08:28:27作者: lnlidawei

python3: dlt - 数据结构2

 

 

 

 

一、源程序

  1 [wit@fedora null]$ cat test.py
  2 #!/usr/bin/env python3
  3 
  4 
  5 
  6 
  7 # test this script
  8 def msg():
  9     print("\nhello, python3!\n")
 10 
 11 
 12 # running msg()
 13 # msg()
 14 
 15 
 16 
 17 
 18 # define class for data
 19 class dlt_data:
 20     def __init__(self):
 21         self.no = 0
 22         self.id = []
 23         self.f = []
 24         self.b = []
 25 
 26     def set(self, uno, uid, ufront, uback):
 27         self.no = uno
 28         self.id = uid
 29         self.f.extend(ufront)
 30         self.b.extend(uback)
 31 
 32     def title(self):
 33         print(f"\n\tNo.\tid\t\tfront\t\t\t\t\t-\tback\t\n")
 34 
 35     def empty_line(sef):
 36         print(f"\n")
 37 
 38     def say(self):
 39         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]}")
 40 
 41 
 42 
 43 
 44 """
 45 dlt = dlt_data()
 46 dlt.set(1, 2023001, [18, 19, 24, 27, 34], [11, 12])
 47 dlt.title()
 48 dlt.say()
 49 """
 50 
 51 
 52 
 53 
 54 # display result for testing
 55 
 56 
 57 """
 58 dlt = []
 59 
 60 
 61 for i in [0,1,2,3,4]:
 62     dlt.append(dlt_data())
 63 i = 0
 64 
 65 
 66 c = 0
 67 for d in dlt:
 68     d.set(c, 2023001+c, [1,12,23,34,35], [1,4])
 69     c = c + 1
 70 c = 0
 71 
 72 
 73 dlt[0].title()
 74 for x in dlt:
 75     x.say()
 76 dlt[0].empty_line()
 77 """
 78 
 79 
 80 
 81 
 82 # define funtion
 83 def speak():
 84 
 85     dlt = []
 86 
 87 
 88     for i in [0,1,2,3,4]:
 89         dlt.append(dlt_data())
 90     i = 0
 91 
 92 
 93     c = 0
 94     for d in dlt:
 95         d.set(c, 2023001+c, [1,12,23,34,35], [1,4])
 96         c = c + 1
 97     c = 0
 98 
 99 
100     dlt[0].title()
101     for x in dlt:
102         x.say()
103     dlt[0].empty_line()
104 
105 
106 
107 
108 # define funtion
109 def speak2():
110 
111     dlt = []
112 
113     scope = 30
114     for i in range(scope):
115         x = dlt_data()
116         x.set(i, 2022001+i, [2, 4, 7, 14, 31], [3, 9])
117         dlt.append(x)
118     i = 0
119 
120 
121     
122     dlt[0].title()
123     for x in dlt:
124         x.say()
125     x = []
126     dlt[0].empty_line()
127     
128 
129 
130 
131 # define function
132 def speak3():
133 
134     dlt = []
135 
136     scope = 5
137     for i in range(scope):
138         x = dlt_data()
139         uf = [ m+i for m in [2,7,8,12,31]]
140         ub = [ n+i for n in [2,8]]
141         x.set(i, 2022001+i, uf, ub)
142         dlt.append(x)
143     i = 0
144 
145 
146     """
147     dlt[0].title()
148     for x in dlt:
149         x.say()
150     x = []
151     dlt[0].empty_line()
152     """
153 
154 
155     return dlt
156 
157 
158 
159 
160 # define  output
161 def output(list_dlt):
162     print(f"\n-- output: start --")
163     for l in list_dlt:
164         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]}")
165     print(f"-- output: end --\n")
166 
167 
168 
169 
170 # define output2
171 def output2(pl):
172 
173 
174     s1 = " "
175     s2 = "  "
176     
177     print(f"\n-- output: start --")
178     for x in pl:
179 
180         f1 = x.f[0] - 1
181         f2 = x.f[1] - x.f[0]
182         f3 = x.f[2] - x.f[1]
183         f4 = x.f[3] - x.f[2]
184         f5 = x.f[4] - x.f[3]
185         f6 = 35 - x.f[4]
186 
187         sp = " - "
188 
189         b1 = x.b[0] - 1
190         b2 = x.b[1] - x.b[0]
191         b3 = 12 - x.b[1]
192 
193 
194         if x.f[0] < 10 :
195             sf1 = s2*f1 + s1
196         else:
197             sf1 = s2*f1
198 
199         if x.f[1] < 10 :
200             sf2 = s2*f2 + s1
201         else:
202             sf2 = s2*f2
203 
204         if x.f[2] < 10 :
205             sf3 = s2*f3 + s1
206         else:
207             sf3 = s2*f3
208 
209         if x.f[3] < 10 :
210             sf4 = s2*f4 + s1
211         else:
212             sf4 = s2*f4
213 
214         if x.f[4] < 10 :
215             sf5 = s2*f5 + s1
216         else:
217             sf5 = s2*f5
218 
219         sf6 = s2*f6
220 
221         if x.b[0] < 10 :
222             sb1 = s2*b1 + s1
223         else:
224             sb1 = s2*b1
225 
226         if x.b[1] < 10 :
227             sb2 = s2*b2 + s1
228         else:
229             sb2 = s2*b2
230 
231         sb3 = s2*b3
232 
233 
234         #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}")
235         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}")
236 
237 
238     print(f"-- output: end --\n")
239 
240 
241     """
242     print(f"\n-- output: start --")
243     for l in pl:
244         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}")
245     print(f"-- output: end --\n")
246     """
247 
248 
249 
250 
251 
252 # test part
253 if  __name__ == "__main__":
254     #speak()
255     #speak2()
256     dlt_list = speak3()
257     output2(dlt_list)
258 [wit@fedora null]$ 
259 [wit@fedora null]$ 

 

 

 

 

二、运行结果

 1 [wit@fedora null]$ ./test.py 
 2 
 3 -- output: start --
 4         0       2022001    2           7   8        12                                      31         -    2             8        
 5         1       2022002      3           8   9        13                                      32       -      3             9      
 6         2       2022003        4           9  10        14                                      33     -        4            10    
 7         3       2022004          5          10  11        15                                      34   -          5            11  
 8         4       2022005            6          11  12        16                                      35 -            6            12
 9 -- output: end --
10 
11 [wit@fedora null]$ 
12 [wit@fedora null]$ 

 

 

 

 

三、参考资料

 

  1、python3: dlt - 数据结构   https://www.cnblogs.com/lnlidawei/p/17825107.html