for x in range(1,10):
for y in range(1,x+1):
print('{} * {} = {}'.format(x, y, x * y),end='\t')
print('\n',end='')
2. 递推表达式:
print('\n'.join(['\t'.join([f'{i} * {j} = {i * j}' for j in range(1, i + 1)]) for i in range(1, 10)]))
这里的嵌套不是for循环的嵌套,而是递推表达式的嵌套。
['\t'.join([f'{i} * {j} = {i * j}' for j in range(1, i + 1)]
这段代码是内层循环,我们可以把这个段代码看做是一个表达式,嵌套在外层的循环体中。其中i的值会在计算外层循环逐次代入列表。双层递推表达式是形成一个嵌套的列表,这里原始的模型应该是如下数据模型:
str_list =[
[1 * 1 = 1],
[2 * 1 = 2 2 * 2 = 4],
[3 * 1 = 3 3 * 2 = 6 3 * 3 = 9],
...
[8 * 1 = 8 8 * 2 = 16 8 * 3 = 24 8 * 4 = 32 8 * 5 = 40 8 * 6 = 48 8 * 7 = 56 8 * 8 = 64]
[9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 9 * 6 = 54 9 * 7 = 63 9 * 8 = 72 9 * 9 = 81]
]
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)