PHP前端开发

周末任务 - 列表

百变鹏仔 5天前 #Python
文章标签 周末

任务:1
s =“a4k3b2”

1) 编写一个程序来获取输出 'abbbbklllbcc'

s = "a4k3b2"output = ""i = 0while 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+=2print(output)

输出:

abbbbklllbcc

2) 编写一个程序来获取输出 'aaaaakkkkbbb'

s = "a4k3b2"output = ""i = 0while i < len(s):    first = s[i]      second =s[i + 1]     if second.isdigit():        output=output+ first+ (int(second)*first)        i+=2print(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']

转置矩阵:矩阵的转置是通过将行改为列、将列改为行来获得的。