开发者

Making a list of all sublists of a list, starting from the beginning of the original list

开发者 https://www.devze.com 2023-04-12 11:01 出处:网络
For example, if x is [1,2,3,4] then my program returns [ [], [1], [1,2], [1,2,3], 开发者_开发知识库[1,2,3,4] ]

For example, if x is [1,2,3,4] then my program returns

     [ [], [1], [1,2], [1,2,3], 开发者_开发知识库[1,2,3,4] ]


That would be:

[x[:i] for i in range(len(x) + 1)]


x = [1, 2, 3, 4]
print [x[:i] for i in xrange(len(x) + 1)]
0

精彩评论

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