任務:1
s =“a4k3b2”
1) 寫一個程式來取得輸出 'abbbbklllbcc'
s = "a4k3b2" output = "" i = 0 while i < len(s): first = s[i] second =s[i + 1] if second.isdigit(): alpha=chr(ord(first)+1) output=output+ first+ (int(second)*alpha) i+=2 print(output)
輸出:
abbbbklllbcc
2) 寫一個程式來取得輸出 'aaaaakkkkbbb'
s = "a4k3b2" output = "" i = 0 while i < len(s): first = s[i] second =s[i + 1] if second.isdigit(): output=output+ first+ (int(second)*first) i+=2 print(output)
輸出:
aaaaakkkbbbb
任務:2
矩陣 = [[10,20,30], [40,50,60], [70,80,90]]
使用綜合 for 和普通 for 迴圈將給定矩陣加入單一清單。
方法:1(使用普通的for迴圈)
matrix = [[10,20,30], [40,50,60], [70,80,90]] output=[] for i in matrix: for j in i: output.append(j) print(output)
方法:2(使用綜合for循環)
matrix = [[10, 20, 30], [40, 50, 60], [70, 80, 90]] output = [j for i in matrix for j in i] print(output)
輸出:
[10, 20, 30, 40, 50, 60, 70, 80, 90]
任務:3
l = ['ABC','DEF', 'GHI', 'JKL']
取得輸出:['ABC', 'def', 'GHI', 'jkl']
l = ['ABC', 'DEF', 'GHI', 'JKL'] output = [] for i, alpha in enumerate(l): if i % 2 != 0: output.append(alpha.casefold()) else: output.append(alpha) print(output)
輸出:
['ABC', 'def', 'GHI', 'jkl']
轉置矩陣:矩陣的轉置是將行改為列、將列改為行來獲得的。
以上是週末任務 - 列表的詳細內容。更多資訊請關注PHP中文網其他相關文章!