Python面试题目之列表推导式应用

题目要求:

生成如下列表

[[0,0,0,0,0,],[0,1,2,3,4,],[0,2,4,6,8,],[0,3,6,9,12,]]

(考察列表生成式和基本逻辑推理)

方法1:

list1 = []
for in range(4):
    temp = []
    for j in range(5):
        temp.append(j*i)
    list1.append(temp)

print(list1)

方法2:

list1 =[[ i*j for j in range(5)] for i in range(4)]
print(list1)

猜你喜欢

转载自www.cnblogs.com/JetpropelledSnake/p/9123384.html