Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split a string into chunks of substrings with successively increasing length

Let's say I have this string:

a = 'abcdefghijklmnopqrstuvwxyz'

And I want to split this string into chunks, like below:

['a', 'bc', 'def', 'ghij', 'klmno', 'pqrstu', 'vwxyz  ']

so that every chunk has a different number of characters. For instance, the first one should have one character, the second two and so on.

If there are not enough characters in the last chunk, then I need to add spaces so it matches the length.

I tried this code so far:

print([a[i: i + i + 1] for i in range(len(a))])

But it outputs:

['a', 'bc', 'cde', 'defg', 'efghi', 'fghijk', 'ghijklm', 'hijklmno', 'ijklmnopq', 'jklmnopqrs', 'klmnopqrstu', 'lmnopqrstuvw', 'mnopqrstuvwxy', 'nopqrstuvwxyz', 'opqrstuvwxyz', 'pqrstuvwxyz', 'qrstuvwxyz', 'rstuvwxyz', 'stuvwxyz', 'tuvwxyz', 'uvwxyz', 'vwxyz', 'wxyz', 'xyz', 'yz', 'z']

Here is my desired output:

['a', 'bc', 'def', 'ghij', 'klmno', 'pqrstu', 'vwxyz  ']
like image 957
U12-Forward Avatar asked Nov 23 '25 03:11

U12-Forward


2 Answers

I don't think any one liner or for loop will look as elegant, so let's go with a generator:

from itertools import islice, count

def get_increasing_chunks(s):
    it = iter(s)
    c = count(1)

    nxt, c_ = next(it), next(c)
    while nxt:
        yield nxt.ljust(c_)
        nxt, c_ = ''.join(islice(it, c_+1)), next(c)

    return out
[*get_increasing_chunks(a)]
# ['a', 'bc', 'def', 'ghij', 'klmno', 'pqrstu', 'vwxyz  ']
like image 150
cs95 Avatar answered Nov 25 '25 15:11

cs95


Thanks to @Prune's comment, I managed to figure out a way to solve this:

a = 'abcdefghijklmnopqrstuvwxyz'
lst = []
c = 0
for i in range(1, len(a) + 1):
    c += i
    lst.append(c)
print([a[x: y] + ' ' * (i - len(a[x: y])) for i, (x, y) in enumerate(zip([0] + lst, lst), 1) if a[x: y]])    
    

Output:

['a', 'bc', 'def', 'ghij', 'klmno', 'pqrstu', 'vwxyz  ']

I find the triangular numbers than do a list comprehension, and add spaces if the length is not right.

like image 39
U12-Forward Avatar answered Nov 25 '25 17:11

U12-Forward



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!