Python 按某个重复索引值拆分列表

Python 按某个重复索引值拆分列表,python,list,indexing,split,element,Python,List,Indexing,Split,Element,我有一个整数列表,其中一些是连续的数字 我所拥有的: myIntList=[21,22,23,24,0,1,2,3,0,1,2,3,4,5,6,7] 我想要的是: MyNewIntList = [[21,22,23,24],[0,1,2,3],[0,1,2,3,4,5,6,7]] 我希望能够按元素0拆分此列表,即在循环时,如果元素为0,则将列表拆分为单独的列表。 然后,在拆分myIntList任意次数后(基于查找元素0的重复次数),我希望将每个“拆分”或连续整数组附加到列表中的一个列表中 我还

我有一个整数列表,其中一些是连续的数字

我所拥有的:

myIntList=[21,22,23,24,0,1,2,3,0,1,2,3,4,5,6,7]

我想要的是:

MyNewIntList = [[21,22,23,24],[0,1,2,3],[0,1,2,3,4,5,6,7]]
我希望能够按元素0拆分此列表,即在循环时,如果元素为0,则将列表拆分为单独的列表。 然后,在拆分
myIntList
任意次数后(基于查找元素0的重复次数),我希望将每个“拆分”或连续整数组附加到列表中的一个列表中

我还能用“字符串列表”而不是整数做同样的事情吗?(根据重复出现的元素将主字符串列表拆分为较小的列表)

编辑:

我如何将列表按连续数字进行拆分?在我的列表中有一部分从322跳到51,中间没有0。我想分开:

[[...319,320,321,322,51,52,53...]]
进入

基本上,如何按连续的数字拆分列表中的元素

张贴于此:

您可以循环浏览整个列表,将其附加到临时列表,直到找到
0
。然后再次重置临时列表并继续

>>> myIntList = [21,22,23,24,0,1,2,3,0,1,2,3,4,5,6,7]
>>> newlist = [] 
>>> templist = []
>>> for i in myIntList:
...      if i==0:
...          newlist.append(templist)
...          templist = []
...      templist.append(i)
... 
>>> newlist.append(templist)
>>> newlist
[[21, 22, 23, 24], [0, 1, 2, 3], [0, 1, 2, 3, 4, 5, 6, 7]]
对于字符串,您可以通过使用
list
调用使用相同的方法

>>> s = "winterbash"
>>> list(s)
['w', 'i', 'n', 't', 'e', 'r', 'b', 'a', 's', 'h']
也使用

您可以使用切片:

myIntList = [21,22,23,24,0,1,2,3,0,1,2,3,4,5,6,7]
myNewIntList = []
lastIndex = 0
for i in range(len(myIntList)):
    if myIntList[i] == 0:
        myNewIntList.append(myIntList[lastIndex:i])
        lastIndex = i

myNewIntList.append(myIntList[lastIndex:])
print(myNewIntList)
# [[21, 22, 23, 24], [0, 1, 2, 3], [0, 1, 2, 3, 4, 5, 6, 7]]
您可以使用
str.split
函数拆分字符串:

s = 'stackoverflow'
s.split('o') # ['stack', 'verfl', 'w'] (removes the 'o's)

import re
[part for part in re.split('(o[^o]*)', s) if part] # ['stack', 'overfl', 'ow'] (keeps the 'o's)
def split_at(i, l):
    it = iter(l)
    out = [next(it)]
    for ele in it:
        if ele != i:
            out.append(ele)
        else:
            yield out
            out = [ele]
    yield out
你可以试试:

i = 0
j = 0
loop = True
newList = []

while loop:
    try:
        i = myIntList.index(0, j)
        newList.append(myIntList[j:i])
        j = i + 1
    except ValueError as e:
        newList.append(myIntList[j:])
        loop = False

print newList
[[21, 22, 23, 24], [1, 2, 3], [1, 2, 3, 4, 5, 6, 7]]
或在函数中:

s = 'stackoverflow'
s.split('o') # ['stack', 'verfl', 'w'] (removes the 'o's)

import re
[part for part in re.split('(o[^o]*)', s) if part] # ['stack', 'overfl', 'ow'] (keeps the 'o's)
def split_at(i, l):
    it = iter(l)
    out = [next(it)]
    for ele in it:
        if ele != i:
            out.append(ele)
        else:
            yield out
            out = [ele]
    yield out
如果您在开始时有一个
0
,它将捕获:

In [89]: list(split_at(0, myIntList))
Out[89]: [[21, 22, 23, 24], [0, 1, 2, 3], [0, 1, 2, 3, 4, 5, 6, 7]]

In [90]: myIntList = [0,21, 22, 23, 24, 0, 1, 2, 3, 0, 1, 2, 3, 4, 5, 6, 7]

In [91]: list(split_at(0, myIntList))
Out[91]: [[0, 21, 22, 23, 24], [0, 1, 2, 3], [0, 1, 2, 3, 4, 5, 6, 7]]
(我隐约怀疑我以前做过,但现在找不到。)

给予

(也就是说,在实践中,我使用了与其他人相同的循环/分支的
yield
版本,但我将发布上述内容以供参考。)

输出

 [[21, 22, 23, 24], [0, 1, 2, 3], [0, 1, 2, 3, 4, 5, 6, 7]]
输出2

myIntList = [0,21,22,23,24,0,1,2,3,0,1,2,3,4,5,6,7]

[[0, 21, 22, 23, 24], [0, 1, 2, 3], [0, 1, 2, 3, 4, 5, 6, 7]]

@米凯萨补充道。这是你想要的吗?@BhargavRao:如果0是第一个元素呢?(在你的itertools版本上。)@DSM是的。没有看到边缘的情况。我很快就会换的。Thanks@DSM我做得很琐碎。请说明是否还有其他更好的方法。@BhargavRao,您应该使用生成器表达式作为第一个表达式,您可以调用
next(gen)
获取第一个元素感谢您的帮助@padraiccningham。我发现定义很有用。谢谢@DSM。编码的多样性总是有用的。:)出于某种原因,我得到了一个importTerror:无法导入名称积累…想法?没关系,我将Python 2改为3,它成功了。感谢
[0,21,22,23,24,0,1,2,3,0,1,2,3,4,5,6,7]
>>> itergroup([21,22,23,24,0,1,2,3,0,1,2,3,4,5,6,7], 0)
[[21, 22, 23, 24], [0, 1, 2, 3], [0, 1, 2, 3, 4, 5, 6, 7]]
>>> itergroup([0,1,2,0,3,4], 0)
[[0, 1, 2], [0, 3, 4]]
>>> itergroup([0,0], 0)
[[0], [0]]
myIntList = [21,22,23,24,0,1,2,3,0,1,2,3,4,5,6,7]

new = []
m,j=0,0
for i in range(myIntList.count(0)+1):
    try:
        j= j+myIntList[j:].index(0)
        if m==j:
           j= j+myIntList[j+1:].index(0)+1



        new.append(myIntList[m:j])
        m,j=j,m+j
    except:
        new.append(myIntList[m:])
        break
print new
 [[21, 22, 23, 24], [0, 1, 2, 3], [0, 1, 2, 3, 4, 5, 6, 7]]
myIntList = [0,21,22,23,24,0,1,2,3,0,1,2,3,4,5,6,7]

[[0, 21, 22, 23, 24], [0, 1, 2, 3], [0, 1, 2, 3, 4, 5, 6, 7]]