4.10.1 逗号代码
def douhao(arg):
return ', '.join(arg[:-1]) + ' and ' + arg[-1]
spam = ['apples', 'bananas', 'tofu', 'cats']
print(douhao(spam))
程序运行结果
apples, bananas, tofu and cats
Process finished with exit code 0
4.10.2 字符图网格
# 打印矩阵grid的转置矩阵
grid = [['.', '.', '.', '.', '.', '.'],
['.', '0', '0', '.', '.', '.'],
['0', '0', '0', '0', '.', '.'],
['0', '0', '0', '0', '0', '.'],
['.', '0', '0', '0', '0', '0'],
['0', '0', '0', '0', '0', '.'],
['0', '0', '0', '0', '.', '.'],
['.', '0', '0', '.', '.', '.'],
['.', '.', '.', '.', '.', '.']]
for x in range(0, len(grid[0])):
for y in range(0, len(grid)):
print(grid[y][x], end=' ')
print('')
程序运行结果
. . 0 0 . 0 0 . .
. 0 0 0 0 0 0 0 .
. 0 0 0 0 0 0 0 .
. . 0 0 0 0 0 . .
. . . 0 0 0 . . .
. . . . 0 . . . .
Process finished with exit code 0
网友评论