2016-06-18 48 views
0

有了:彙編列表理解

A = [7, 15, 21] 
print [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]]這不是我想要的。

什麼是Python的做法?

回答

1

您可以使用雙迴路列表解析裏:

A = [7, 15, 21] 
B = [b for i in A for b in (i, i + 1, i + 2)] 
# [7, 8, 9, 15, 16, 17, 21, 22, 23]