标签: python list
使用:
A = [7, 15, 21] print [i, i+1, i+2 for i in A]
我想获得[7, 8, 9, 15, 16, 17, 21, 22, 23]。
[7, 8, 9, 15, 16, 17, 21, 22, 23]
当然这样做并不起作用,而[[i, i+1, i+2] for i in A]会给[[7, 8, 9], [15, 16, 17], [21, 22, 23]]这不是我想要的。
[[i, i+1, i+2] for i in A]
[[7, 8, 9], [15, 16, 17], [21, 22, 23]]
这样做的pythonic方式是什么?
答案 0 :(得分:1)
您可以在列表理解中使用双循环:
BLOB