按照指定的重复索引值分割列表

8

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

我的列表:

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...]]

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

基本上,我如何通过连续的数字来拆分列表中的元素?

在此发布: 按连续顺序拆分列表(整数)为单独的列表

6个回答

5
it  = iter(myIntList)
out = [[next(it)]]
for ele in it:
    if ele != 0:
        out[-1].append(ele)
    else:
        out.append([ele])

print(out)

或者在一个函数中:

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]]

1
感谢@PadraicCunningham的帮助。我发现定义很有用。 - Mike Issa

3

我模糊地怀疑我以前做过这件事,但现在找不到了。

from itertools import groupby, accumulate

def itergroup(seq, val):
    it = iter(seq)    
    grouped = groupby(accumulate(x==val for x in seq))
    return [[next(it) for c in g] for k,g in grouped]

提供

>>> 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]]

(话虽如此,在实际中我使用与其他人相同的基于yield的循环/分支,但为了多样性,我将上述内容发布。)


感谢 @DSM。编程中的多样性总是有用的。 :) - Mike Issa
由于某些原因,我遇到了一个 ImportError: cannot import name accumulate 的错误...有什么想法吗? - Mike Issa
没事了,我把Python 2改成了3,现在可以用了。谢谢。 - Mike Issa

2
您可以使用切片操作:
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)

1
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

output

 [[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]]

1
尝试 [0,21,22,23,24,0,1,2,3,0,1,2,3,4,5,6,7] - Padraic Cunningham

0

您可以循环遍历整个列表,将元素添加到临时列表中,直到找到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']

还可以使用itertools

>>> import itertools
>>> myIntList = [21,22,23,24,0,1,2,3,0,1,2,3,4,5,6,7]
>>> temp=[list(g) for k,g in itertools.groupby(myIntList,lambda x:x== 0) if not k]
>>> if myIntList[0]!=0:
...     newlist = [temp[0]] + [[0]+i for i in temp[1:]]
... else:
...     newlist = [[0]+i for i in temp]
... 
>>> newlist
[[21, 22, 23, 24], [0, 1, 2, 3], [0, 1, 2, 3, 4, 5, 6, 7]]

1
@MikeIssa 已添加。这是你想要的吗? - Bhargav Rao
1
@BhargavRao:如果0是第一个元素怎么办?(关于你的itertools版本。) - DSM
1
@Padraic 猜想那将使它类似于你的答案。无论如何,我会编辑它。 - Bhargav Rao
1
@BhargavRao,我的意思是使用groupby解决方案,这样就不会存储多份数据了。 - Padraic Cunningham
1
@PadraicCunningham 谢谢,我马上编辑。 - Bhargav Rao
显示剩余8条评论

-3

你可以尝试:

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]]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接