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

时间:2015-12-14 15:48:14

标签: 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的重复)之后,我想要追加每个'分裂'或一组连续的整数到列表中的列表中。

我也可以使用'字符串列表来做同样的事情。而不是整数? (根据重复出现的元素将主字符串列表拆分为较小的列表)

编辑:

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

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

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

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

发表在这里: Split list of lists (integers) by consecutive order into separate lists

6 个答案:

答案 0 :(得分:4)

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 :(得分: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版本,但是我会发布上面的内容以供变化。)

答案 2 :(得分: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)

答案 3 :(得分: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

<强>输出

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

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

答案 4 :(得分: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]]

答案 5 :(得分:-3)

你可以尝试:

public Connection getConnection(String user, String pass) {
  String url = "jdbc:postgresql://localhost:5432/bd";
  return DriverManager.getConnection(url, user, pass);
}