小编fra*_*g43的帖子

How to cut a list by specific item?

I'm trying to cut a list by specific items in it, for example, I have a list like this:

down = ["a", "b", "c", "d", "b", "e", "r"]
Run Code Online (Sandbox Code Playgroud)

What I want is:

[["a", "b"]["c", "d", "b"] ["e", "r"]]
Run Code Online (Sandbox Code Playgroud)

which is cut after every occurrence of "b".

I wrote something like this:

down = ["a", "b", "c", "d", "b", "e", "r"]
up = []
while down is not []:
    up, down = up.append(down[:(down.index("b") + 1)]), down[(down.index("b") + 1):]
Run Code Online (Sandbox Code Playgroud)

It throws …

python list

7
推荐指数
1
解决办法
2248
查看次数

标签 统计

list ×1

python ×1