7sh.cn 7sh.cn

欢迎光临
我们一直在努力
顶部
域名
云服务器48/月

周末任务 - 列表-Python教程-

任务: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']
登录后复制

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

image description

以上就是周末任务 - 列表的详细内容,更多请关注php中文网其它相关文章!

【声明】:本博客不参与任何交易,也非中介,仅记录个人感兴趣的主机测评结果和优惠活动,内容均不作直接、间接、法定、约定的保证。访问本博客请务必遵守有关互联网的相关法律、规定与规则。一旦您访问本博客,即表示您已经知晓并接受了此声明通告。
发布内容
-六神源码网 网站出售带数据-六神源码网 网站出售带数据-六神源码网 网站出售带数据-六神源码网