2024-10-14_simple-python-tricks-9.html


001| #:name::Simple Python Tricks #9
002| #:link::https://thehardcorecoder.com/2024/10/14/simple-python-tricks-9/
003| #:date::2024-10-14
004| #:code::
005| words = ['apple', 'orange', 'grape', 'lemon', 'banana']
006| 
007| for ix in range(len(words)):
008|     value = words[ix]
009|     print(f'{ix+1:2d}: {value}')
010| print()
011| 
012| #:code::
013| words = ['apple', 'orange', 'grape', 'lemon', 'banana']
014| 
015| for ix,value in enumerate(words):
016|     print(f'{ix+1:2d}: {value}')
017| print()
018| 
019| #:code::
020| words = ['apple', 'orange', 'grape', 'lemon', 'banana']
021| 
022| for value in words:
023|     print(f'value is: {value}')
024| print()
025| 
026| #:code::
027| tic_tac_toe = [[0,0,0], [0,0,0], [0,0,0]]
028| 
029| for x in range(3):
030|     for y in range(3):
031|         value = tic_tac_toe[x][y]
032|         print(f'[{x+1},{y+1}] = {value}')
033|     print()
034| print()
035| 
036| #:code::
037| tic_tac_toe = [[0,0,0], [0,0,0], [0,0,0]]
038| 
039| for x,row in enumerate(tic_tac_toe):
040|     for y,value in enumerate(row):
041|         print(f'[{x+1},{y+1}] = {value}')
042|     print()
043| print()
044| 
045| #:code::
046| words = ['apple', 'orange', 'grape', 'lemon', 'banana']
047| 
048| for ix,value in enumerate(words, start=1):
049|     print(f'{ix:2d}: {value}')
050| print()
051| 
052| #:code::
053| words = ['apple', 'orange', 'grape', 'lemon', 'banana']
054| 
055| for value in sorted(words):
056|     print(value)
057| print()
058| 
059| #:code::
060| words = ['apple', 'orange', 'grape', 'lemon', 'banana']
061| 
062| for ix,value in enumerate(sorted(words), start=1):
063|     print(f'{ix:2d}: {value}')
064| print()
065| 
066| #:code::
067| words = ['Drew', 'Blair', 'Alex', 'Em', 'Chris', 'Fran']
068| 
069| for value in sorted(words, reverse=True):
070|     print(value)
071| print()
072| 
073| #:code::
074| records = [
075|     ('Gene' , 'R', 'Smith', 'grsmith@vmail.not', 87),
076|     ('Drew' , 'T', 'Smith', 'dtsmith@vmail.not', 120),
077|     ('Blair', 'D', 'Jones', 'jonsey@aol.not', 173),
078|     ('Blair', 'M', 'Green', 'bmgreen53@notacorp.not', 152),
079|     ('Alex' , 'G', 'Johnson', 'alex.g@mobile.not', 93),
080|     ('Em'   , 'A', 'Nother' , 'emmy@highermaths.not', 248),
081|     ('Chris', 'J', 'Green', 'cjgreen47@mickysoft.not', 137),
082|     ('Fran' , 'C', 'Xavier', 'profx@xmenhq.not', 203),
083| ]
084| fmt = '%-6s %s %-8s %-24s %3d'
085| 
086| for value in sorted(records):
087|     print(fmt % (*value,))
088| print()
089| 
090| for value in sorted(records, key=lambda t:t[2]):
091|     print(fmt % (*value,))
092| print()
093| 
094| for value in sorted(records, key=lambda t:(t[2],t[0])):
095|     print(fmt % (*value,))
096| print()
097| 
098| #:code::
099| names = """
099| 1: Gene   R Smith    grsmith@vmail.not         87
099| 2: Drew   T Smith    dtsmith@vmail.not        120
099| 3: Blair  D Jones    jonsey@aol.not           173
099| 4: Blair  M Green    bmgreen53@notacorp.not   152
099| 5: Alex   G Johnson  alex.g@mobile.not         93
099| 6: Em     A Nother   emmy@highermaths.not     248
099| 7: Chris  J Green    cjgreen47@mickysoft.not  137
099| 8: Fran   C Xavier   profx@xmenhq.not         203
099| """

100| namelist = names.splitlines()
101| 
102| def sortfunc (line):
103|     field1 = line[12:21].rstrip()
104|     field2 = line[3:10].rstrip()
105|     return f'{field1} {field2}'
106| 
107| for name in sorted(namelist, key=sortfunc):
108|     print(name)
109| print()
110| 
111| #:code::
112| basechr = ord('A')
113| letters = [chr(basechr+cx) for cx in range(12)]
114| 
115| rletters = list(reversed(letters))
116| 
117| print(letters)
118| print(rletters)
119| print()
120| 
121| rhello = list(reversed('Hello, World!'))
122| 
123| print(rhello)
124| print(''.join(rhello))
125| print()
126| 
127| #:code::
128| records = [
129|     ('Gene' , 'R', 'Smith', 'grsmith@vmail.not', 87),
130|     ('Drew' , 'T', 'Smith', 'dtsmith@vmail.not', 120),
131|     ('Blair', 'D', 'Jones', 'jonsey@aol.not', 173),
132|     ('Blair', 'M', 'Green', 'bmgreen53@notacorp.not', 152),
133|     ('Alex' , 'G', 'Johnson', 'alex.g@mobile.not', 93),
134|     ('Em'   , 'A', 'Nother' , 'emmy@highermaths.not', 248),
135|     ('Chris', 'J', 'Green', 'cjgreen47@mickysoft.not', 137),
136|     ('Fran' , 'C', 'Xavier', 'profx@xmenhq.not', 203),
137| ]
138| fmt = '%-6s %s %-8s %-24s %3d'
139| 
140| def fix_middle_initial (r):
141|     mi = f'{r[1]}.'
142|     return (r[0], mi, *r[2:])
143| 
144| records2 = map(fix_middle_initial, records)
145| 
146| for rcd in records2:
147|     print(fmt % (*rcd,))
148| print()
149| 
150| #:code::
151| records = [
152|     ('Gene' , 'R', 'Smith', 'grsmith@vmail.not', 87),
153|     ('Drew' , 'T', 'Smith', 'dtsmith@vmail.not', 120),
154|     ('Blair', 'D', 'Jones', 'jonsey@aol.not', 173),
155|     ('Blair', 'M', 'Green', 'bmgreen53@notacorp.not', 152),
156|     ('Alex' , 'G', 'Johnson', 'alex.g@mobile.not', 93),
157|     ('Em'   , 'A', 'Nother' , 'emmy@highermaths.not', 248),
158|     ('Chris', 'J', 'Green', 'cjgreen47@mickysoft.not', 137),
159|     ('Fran' , 'C', 'Xavier', 'profx@xmenhq.not', 203),
160| ]
161| fmt = '%-6s %s %-8s %-24s %3d'
162| 
163| def exclude_low_values (r):
164|     return (150 < r[4])
165| 
166| records2 = filter(exclude_low_values, records)
167| 
168| for rcd in records2:
169|     print(fmt % (*rcd,))
170| print()
171| 
172| #:code::
173| a = [1, 2, 3, 4, 5, 6]
174| b = ['A', 'B', 'C', 'D', 'E']
175| c = ['z', 'y', 'x', 'w', 'v', 'u']
176| 
177| for t in zip(a,b,c):
178|     print(t)
179| print()
180| 
181| #:code::
182| a = [1, 2, 3, 4, 5, 6]
183| b = ['A', 'B', 'C', 'D', 'E']
184| c = ['z', 'y', 'x', 'w', 'v', 'u']
185| 
186| for t in zip(a,b,c, strict=True):
187|     print(t)
188| print()
189| 
190| #:code::
191| a = [1, 2, 3]
192| b = 'ABC'
193| c = 'zyx'
194| 
195| z = zip(a,b,c)
196| 
197| for t in zip(*z):
198|     print(t)
199| print()
200| 
201| #:code::
202| nums = [1, 2, 3, 4, 5, 6]
203| reps = 4
204| 
205| for t in zip(*[nums]*reps, strict=True):
206|     print(t)
207| print()
208| 
209| #:code::
210| t3a = [[0,0,0], [0,0,0], [0,0,0]]
211| t3b = [[0]*3, [0]*3, [0]*3]
212| t3c = [[0]*3]*3
213| t3d = [[0 for y in range(3)] for x in range(3)]
214| 
215| print(f't3a = {t3a}')
216| print(f't3b = {t3b}')
217| print(f't3c = {t3c}')
218| print(f't3d = {t3d}')
219| print()
220| 
221| t3a[1][1] = 1
222| t3b[1][1] = 1
223| t3c[1][1] = 1
224| t3d[1][1] = 1
225| 
226| print(f't3a = {t3a}')
227| print(f't3b = {t3b}')
228| print(f't3c = {t3c}')
229| print(f't3d = {t3d}')
230| print()
231| 
232| #:end:code::
233|