MaxDev
MaxDev

Reputation: 25

Is there a way to split a string into chunks and store in a list python

I have a string

 x = "yfbrtutcfyugytfytfytcfdycfyrcdtrdrcdtreextredydsadyradrydstrdfrdfrrdecrcxhx"

and I wanna split the string x into the following list.

y = [
    "yfbrtutcfyugytfyt",
    "cfdycfyrcdtrdrcdtreextredy",
    "strdfrdfrrdecrcx"
]

How can I do this by providing the number 3 and have it split into a list of the length 3? The length of the text could be both odd and even.

Upvotes: 0

Views: 128

Answers (6)

Ajay
Ajay

Reputation: 5347

n=10
num=int(len(x) / n) + (len(x) % n > 0)
y=[x[i:i+num] for i in range(0, len(x),num)]

Output:

['yfbrtutc',
 'fyugytfy',
 'tfytcfdy',
 'cfyrcdtr',
 'drcdtree',
 'xtredyds',
 'adyradry',
 'dstrdfrd',
 'frrdecrc',
 'xh']

Upvotes: 1

Correy Koshnick
Correy Koshnick

Reputation: 211

I don't know of a python built-in function that does this.

The function below divides a long string into N chunks.

def split_string_N(string, N):
    chunk_size = len(string) / N
    return [string[int(n * chunk_size): int((n+1) * chunk_size)] for n in range(N)]

test_string = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

chunks = split_string_N(test_string, 10)
print(chunks)

['abcde', 'fghij', 'klmno', 'pqrst', 'uvwxyz', 'ABCDE', 'FGHIJ', 'KLMNO', 'PQRST', 'UVWXYZ']

Notice that since this is not equally divisible by 10 that some chunks are length 5 (pqrst) and some length 6 (uvwxyz), and it occurs 'naturally' where the remainder rolls over to the next higher integer. Also, it will give bad output where N > len(string). You can add some logic to prevent that if you want :) Gl!

Upvotes: 0

bilakos
bilakos

Reputation: 145

This is my perspective of solution on your problem

x = "yfbrtutcfyugytfytfytcfdycfyrcdtrdrcdtreextredydsadyradrydstrdfrdfrrdecrcxhx"
length = len(x)
list_x = []
count = 0
for i in range(3):
    count += 1
    if count == 1:
        X = x[:(length // 3)]
        list_x.append(X)
    elif count == 2:
        X = x[(length // 3):((length // 3)*2)]
        list_x.append(X)
    else:
        X = x[((length // 3)*2):length]
        list_x.append(X)
print(list_x)

And you get those results:

['yfbrtutcfyugytfytfytcfdyc', 'fyrcdtrdrcdtreextredydsad', 'yradrydstrdfrdfrrdecrcxhx']

Upvotes: 1

Pygirl
Pygirl

Reputation: 13349

I hope you are creating random test cases by splitting into 3 parts of any length.

You can try using random:

import random as rnd
x = "yfbrtutcfyugytfytfytcfdycfyrcdtrdrcdtreextredydsadyradrydstrdfrdfrrdecrcxhx"
len_x = len(x)
idx1 = rnd.randint(0,len_x-1)
idx2 = rnd.randint(idx1,len_x)
l = [x[0:idx1], x[idx1:idx2], x[idx2:len_x]]

l:

['yfbrtutcfyugytfytfytcfdycfyrcdtrdrcdtreextredydsadyrad',
 'rydstrdfrdfrr',
 'decrcxhx']

Upvotes: 1

Vidya Ganesh
Vidya Ganesh

Reputation: 818

You can make use of the split function in python

word = 'yfbrtutcfyugytfytfytcfdycfyrcdtrdrcdtreextredydsadyradrydstrdfrdfrrdecrcxhx'
x=round(len(word)/3)
print([word[i:i+x] for i in range(0, len(word),x)]) 

And it gives you:

['yfbrtutcfyugytfytfytcfdyc', 'fyrcdtrdrcdtreextredydsad', 'yradrydstrdfrdfrrdecrcxhx']

Upvotes: 1

user122644
user122644

Reputation: 109

You can use x[start_index:end_index] which yields a substring from x (last index excluded). So just specify 2 positions and use them e.g.:

a = len(x) // 3
b = 2* len(x) // 3
s0 = x[:a]
s1 = x[a:b]
s2 = x[b:]

Upvotes: 1

Related Questions