Milanowitsj
Milanowitsj

Reputation: 33

Split a string with list of numbers

I'm trying to split a string by the positions given from a list, and add the parts to a new list. I start with:

seq = 'ATCGATCGATCG'
seq_new = []
seq_cut = [2, 8 , 10]

I would like to get:

seq_new = ['AT', 'CGATCG', 'AT', 'CG'] 

The list with the positions is variable in size and values. How can I process my data like this?

Upvotes: 3

Views: 124

Answers (2)

Steven Rumbalski
Steven Rumbalski

Reputation: 45542

Use zip to create indexes for slicing:

seq_new = [seq[start:end] for start, end in zip([None] + seq_cut, seq_cut + [None])]

This zips together [None, 2, 8 , 10] and [2, 8, 10, None] to create the indexes [(None, 2), (2, 8), (8, 10), (10, None)]. None as first index defaults to zero, None as the second index defaults to the size of the sequence being sliced.

Upvotes: 7

orlp
orlp

Reputation: 117691

Use slicing:

seq = "ATCGATCGATCG"
seq_new = []
seq_cut = [2, 8, 10]

last = 0
for idx in seq_cut:
    seq_new.append(seq[last:idx])
    last = idx
seq_new.append(seq[last:])

Upvotes: 4

Related Questions