任务: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中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号