任务: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']
登录后复制
转置矩阵:矩阵的转置是通过将行改为列、将列改为行来获得的。
以上就是周末任务 - 列表的详细内容,更多请关注其它相关文章!
Article Links:https://www.hinyin.com/n/309933.html
Article Source:admin
Article Copyright:除非特别标注,否则均为本站原创文章,转载时请以链接形式注明文章出处。