简体   繁体   English

按某些重复索引值拆分列表

[英]split list by certain repeated index value

I have a list of integers, in which some are consecutive numbers. 我有一个整数列表,其中一些是连续的数字。

What I have: 是)我有的:

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

What I want: 我想要的是:

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

I want to be able to split this list by the element 0, ie when looping, if the element is 0, to split the list into separate lists. 我希望能够通过元素0拆分此列表,即在循环时,如果元素为0,则将列表拆分为单独的列表。 Then, after splitting myIntList whatever number of times (based on the recurrences of finding the element 0), I want to append each 'split' or group of consecutive integers into a list within a list. 然后,在分割myIntList任意次数(基于找到元素0的重复)之后,我想将每个“split”或一组连续整数附加到列表中的列表中。

Also would I be able to do the same sort of thing with a 'list of strings' instead of integers? 我还可以使用'字符串列表'而不是整数来做同样的事情吗? (Split the main string list into smaller lists based on a reoccurring element) (根据重复出现的元素将主字符串列表拆分为较小的列表)

EDIT: 编辑:

How would I go about splitting the list by consecutive numbers? 我如何按连续数字拆分列表? There's a part in my list where it jumps from 322 to 51, there is no 0 in between. 我的列表中有一部分从322跳到51,中间没有0。 I want to split: 我想拆分:

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

into

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

basically, how do I split elements in a list by consecutive numbers? 基本上,如何按连续数字拆分列表中的元素?

Posted here: Split list of lists (integers) by consecutive order into separate lists 发布在此处: 按顺序拆分列表(整数)到单独的列表中

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

print(out)

Or in a function: 或者在一个功能中:

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

It will catch if you have a 0 at the start: 如果你在开始时有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]]

(I vaguely suspect I've done this before but I can't find it now.) (我隐约怀疑我以前做过这个,但我现在找不到它。)

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]

gives

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

(That said, in practice I use the yield version of the same loop/branch that everyone else does, but I'll post the above for variety.) (也就是说,在实践中我使用与其他人相同的循环/分支的yield版本,但是我会发布上面的变种。)

You can use slicing: 你可以使用切片:

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

You can split strings using the str.split function: 您可以使用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)
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]]

output2 OUTPUT2

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

You can loop through the whole list, appending to a temp list till 0 is found. 您可以遍历整个列表,附加到临时列表,直到找到0 Then you again reset the temp list and continue. 然后再次重置临时列表并继续。

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

and for strings you can use the same approach by using the list call 对于字符串,您可以使用list调用使用相同的方法

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

Also using itertools 也使用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]]

You could try: 你可以尝试:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM