开发者

python: append only specific elements from a list

i have a list of开发者_运维百科 a list:

b=[[1,2,3],[4,5,6],[7,8,9]]

i have a list:

row = [1,2,3]

how do i append to b only row[0] and '3847' and row[2] such that b will equal:

b=[[1,2,3],[4,5,6],[7,8,9],[1,3847,3]]


You're going to have to be more specific.

This will accomplish what you want:

b.append([row[0], 3847, row[2]])

But isn't really a general solution.


b.append([ x if x != 2 else 3847 for x in row])


b + [[row[0],3847,row[2]]]


b + [row[0],3847,row[2]] would give you:

>>> b + [row[0],3847,row[2]]
[[1, 2, 3], [4, 5, 6], [7, 8, 9], 1, 3847, 3]

In order to get b=[[1,2,3],[4,5,6],[7,8,9],[1,3847,3]], you need to use append as suggested by "Nick Presta". You may have received other suitable solutions if you made the problem statement clearer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜