0

For example, string 0123456789 with input 1,2,6,1 would be:

[0,12,345678,9]
7
  • 1
    Part of your "first steps" should be with experimenting in writing code. If you've done that, you should show your attempts. Commented Oct 27, 2016 at 8:06
  • 1
    @Sayse I'm not a beginner that's learning programming or algorithms, just getting myself into a python mindset. Hence all the show-your-attempt attitude is unhelpful, especially for such a trivial task where I'm only interested in Python elegance and modern libraries I'm not aware of. Commented Oct 27, 2016 at 8:29
  • 2
    @susdu On the sidebar of the ask-a-question page is Provide details. Share your research. Show some effort no matter how little next time Commented Oct 27, 2016 at 8:30
  • 1
    Why are Stack Overflow users more passionate about Stack Overflow than helping people? Commented Oct 27, 2016 at 8:34
  • 3
    There's a difference between "I have no idea how to do this" and "I know how to do it the ugly way, I'm looking for a pythonic solution" and answers have to be very different for these two kinds of people. We don't know which you are unless you show us something to judge you by. Commented Oct 27, 2016 at 8:40

3 Answers 3

3

One way to do this, is with itertools.islice:

from itertools import islice

chunks = (1,2,6,1)
s = '0123456789'
assert len(s) >= sum(chunks)

it = iter(s)
result = [int(''.join(islice(it, i))) for i in chunks]
print(result)
# [0, 12, 345678, 9]
Sign up to request clarification or add additional context in comments.

2 Comments

It's controversial to answer questions when the questioner shows zero research effort, but I consider others who will come over to SO looking for a solution to this particular problem.
It didn't take too long to find a duplicate
0

Not so pythonic but funny to write:

def split(a,*b):
    def split_rec(a,b,l):
        print a
        if len(b) == 0:
            return l
        cut_point = b[0]
        l.append(a[0:cut_point])
        return split_rec(a[cut_point:], b[1:], l)
    return split_rec(a,b,[])

print split("1234567890",1,2,6,1)

Comments

0

Another simplistic way of achieving the result using basic code.

a="0123456789"
inp=[1,2,6,1]
start_pos=[0]               #Fix first start position
x=0
for i in inp:               #Calculate start positions of slices
    x=x+i
    start_pos.append(x)
res=[]
for i in range(len(inp)):   #Cut out slices
    res.append(int(a[start_pos[i]:start_pos[i]+inp[i]]))
print (res) 

[0, 12, 345678, 9]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.