4

I want to split a list that looks something like this:

list = [5, a, b, c, d, e, 2, a, b, 4, a ,b ,c ,d , ...]

into this:

list  = [ [5, a, b, c, d, e], [2, a, b] , [4, a ,b ,c ,d] ...]

The first element/number is variable, so no pattern to split it in even chunks. The chunks size or length should be based on that first element of the chunk. Also the alphabetic letters are just placeholders to make the example more readable, in reality the alphabetic letters are floats and numbers.

So the big list really looks something like this:

list = [5, 7, 3.2, 3.1, 4.6, 3, 2, 5.1, 7.1, 4, 5.12 ,3.4 ,4.8 ,12.1 , ...]
2
  • 2
    Since you haven't made any attempt, here's a solution with pandas for you. x = pd.Series(lst); result = x.groupby(x.astype(str).str.isdigit().cumsum()).apply(list).tolist() Good luck!
    – cs95
    Dec 16, 2017 at 22:24
  • 1
    I disagree with the duplicate target. The answers from there aren't directly applicable here.
    – vaultah
    Dec 16, 2017 at 22:28

3 Answers 3

19

Simple way, read each chunk length n and include the next n values:

it = iter(l)
[[n, *islice(it, n)] for n in it]

Python 2 version:

it = iter(l)
[[n] + list(islice(it, n)) for n in it]

Or without islice:

it = iter(l)
[[n] + [next(it) for _ in range(n)] for n in it]

Demo:

>>> from itertools import islice
>>> l = [5, 'a', 'b', 'c', 'd', 'e', 2, 'f', 'g', 4, 'h' ,'i' ,'j' ,'k']
>>> it = iter(l)
>>> [[n, *islice(it, n)] for n in it]
[[5, 'a', 'b', 'c', 'd', 'e'], [2, 'f', 'g'], [4, 'h', 'i', 'j', 'k']]
0
2

You can try this one:

l = [5, 'a', 'b', 'c', 'd', 'e', 2, 'a', 'b', 4, 'a' ,'b' ,'c' ,'d']

pos = [(index, i) for index, i in enumerate(l) if type(i)==int]
l = [l[p[0]:p[0]+p[1]+1] for p in pos]
print(l)

Output:

[[5, 'a', 'b', 'c', 'd', 'e'], [2, 'a', 'b'], [4, 'a', 'b', 'c', 'd']]
2
  • 1
    The last list does not contain the last item. But nevertheless, please do not feed trolls... Dec 16, 2017 at 22:31
  • Doesn't work if there are ints among the "placeholders". Like there are in their explicit example. Dec 16, 2017 at 23:04
1

Could also trying something simple like this:

l = [5, 'a', 'b', 'c', 'd', 'e', 2, 'f', 'g', 4, 'h' ,'i' ,'j' ,'k']

numbers = [i for i, e in enumerate(l) if isinstance(e, int)]

result = [l[s:e] for s, e in zip(numbers[:-1], numbers[1:])] + [l[numbers[-1]:]]

print(result)
# [[5, 'a', 'b', 'c', 'd', 'e'], [2, 'f', 'g'], [4, 'h', 'i', 'j', 'k']]

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.