Splitting a list

I

Ian Sparks

string.split() is very useful, but what if I want to split a list of integers on some element value?

e.g. :
l = [1,2,3,-1,4,5,-1,8,9]
l.split(-1)
[[1,2,3],[4,5],[8,9]]

Here's my hideous first pass :
[[int(z) for z in x.split(',') if z] for x in ','.join([str(a) for a in l]).split('-1')]
[[1, 2, 3], [4, 5], [8, 9]]

When I see code like that I just know I've missed something obvious....
 
M

Michael J. Fromberger

"Ian Sparks said:
string.split() is very useful, but what if I want to split a list of integers
l = [1,2,3,-1,4,5,-1,8,9]
l.split(-1)
[[1,2,3],[4,5],[8,9]]

Here's my hideous first pass :
[[int(z) for z in x.split(',') if z] for x in ','.join([str(a) for a in
l]).split('-1')]
[[1, 2, 3], [4, 5], [8, 9]]

When I see code like that I just know I've missed something obvious....

How about this?

def split_list(L, brk):
last = -1
out = []

for pos in [ x for (x, y) in enumerate(L) if y == brk ]:
out.append(L[last + 1 : pos])
last = pos

out.append(L[last + 1:])
return out

-M
 
P

Paul McGuire

string.split() is very useful, but what if I want to split a list of
integers on some element value?

e.g. :
l = [1,2,3,-1,4,5,-1,8,9]
l.split(-1)
[[1,2,3],[4,5],[8,9]]

Here's my hideous first pass :
[[int(z) for z in x.split(',') if z] for x in ','.join([str(a) for a in l]).split('-1')]
[[1, 2, 3], [4, 5], [8, 9]]

When I see code like that I just know I've missed something obvious....



Maybe not the prettiest, but here's a solution using a generator (at least
you're avoiding a bunch of conversions to/from string).

-- Paul


l = [1,2,3,-1,4,5,-1,8,9]

def getSublists(lst, delim):
loc = 0
while loc < len(lst):
try:
nextloc = lst.index(delim,loc)
except ValueError:
nextloc = len(lst)
yield lst[loc:nextloc]
loc = nextloc+1

print [ sub for sub in getSublists(l,-1) ]
 
E

Elaine Jackson

def indices(x,y):
if y in x:
i = x.index(y)
j = i+1
return +[z+j for z in indices(x[j:],y)]
return []

def listSplit(x,y):
z = [-1] + indices(x,y) + [len(x)]
return [x[z+1:z[i+1]] for i in range(len(z)-1)]


string.split() is very useful, but what if I want to split a list of integers on
some element value?

e.g. :
l = [1,2,3,-1,4,5,-1,8,9]
l.split(-1)
[[1,2,3],[4,5],[8,9]]

Here's my hideous first pass :
[[int(z) for z in x.split(',') if z] for x in ','.join([str(a) for a in l]).split('-1')]
[[1, 2, 3], [4, 5], [8, 9]]

When I see code like that I just know I've missed something obvious....
 
A

Andrea Griffini

string.split() is very useful, but what if I want to split a list of integers on some element value?

Here are a few early-morning attempts :)

def lsplit(L,sep):
try:
i = L.index(sep)
return [L[:i]] + lsplit(L[i+1:],sep)
except ValueError:
return [L]

def lsplit2(L,sep):
i = 0
res = []
while True:
try:
j = L.index(sep,i)
res.append(L[i:j])
i = j+1
except ValueError:
res.append(L[i:])
break
return res

def lsplit3(L,sep):
i = 0
while True:
try:
j = L.index(sep,i)
yield L[i:j]
i = j+1
except ValueError:
yield L[i:]
break


HTH
Andrea
 
A

Alex Martelli

Ian Sparks said:
string.split() is very useful, but what if I want to split a list of
integers on some element value?
e.g. :
l = [1,2,3,-1,4,5,-1,8,9]
l.split(-1)
[[1,2,3],[4,5],[8,9]]

Here's my hideous first pass :
[[int(z) for z in x.split(',') if z] for x in ','.join([str(a) for a in l]).split('-1')]
[[1, 2, 3], [4, 5], [8, 9]]

When I see code like that I just know I've missed something obvious....

I think a simple generator might serve you well:

def isplit(seq, separator):
result = []
for item in seq:
if item == separator:
yield result
result = []
else:
result.append(item)
yield result

example use:
list(isplit([1,2,3,-1,4,5,-1,8,9], -1))
[[1, 2, 3], [4, 5], [8, 9]]

Note that, the way isplit is coded, seq can be any iterable, not just a
list. You may not need this little extra generality, but it can't
hurt...


Alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top