按某些重复索引值拆分列表
split list by certain repeated index value
我有一个整数列表,其中一些是连续的数字。
我有:
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 的重复次数),我想将每个 'split' 或一组连续整数附加到列表中的列表中。
我也可以用 'list of strings' 而不是整数来做同样的事情吗? (根据重复出现的元素将主字符串列表拆分为更小的列表)
编辑:
我将如何按连续数字拆分列表?我的列表中有一部分从 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
您可以遍历整个列表,附加到临时列表直到找到 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]]
您可以使用切片:
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 = 'Whosebug'
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)
你可以试试:
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]]
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]]
(我隐约怀疑我以前做过这个,但我现在找不到了。)
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]]
(也就是说,在实践中我使用与其他人相同的 loop/branch 的 yield
版本,但我将 post 上面的版本用于变化。)
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]]
我有一个整数列表,其中一些是连续的数字。
我有:
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 的重复次数),我想将每个 'split' 或一组连续整数附加到列表中的列表中。
我也可以用 'list of strings' 而不是整数来做同样的事情吗? (根据重复出现的元素将主字符串列表拆分为更小的列表)
编辑:
我将如何按连续数字拆分列表?我的列表中有一部分从 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
您可以遍历整个列表,附加到临时列表直到找到 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]]
您可以使用切片:
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 = 'Whosebug'
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)
你可以试试:
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]]
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]]
(我隐约怀疑我以前做过这个,但我现在找不到了。)
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]]
(也就是说,在实践中我使用与其他人相同的 loop/branch 的 yield
版本,但我将 post 上面的版本用于变化。)
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]]