Home > Back-end >  Split string by Nth occurrence of a character
Split string by Nth occurrence of a character

Time:07-15

I have a string with some comma-separated values:

my_string = 'abc,kjj,hg,kj,ls,jsh,ku,lo,sasad,hh,da'

I need to split this string by every 5th occurrence of a comma.

Code I tried:

a = re.findall("\,".join(["[^,] "] * 5), my_string)

Current output:

['abc,kjj,hg,kj,ls', 'jsh,ku,lo,sasad,hh']

Expected output:

['abc,kjj,hg,kj,ls', 'jsh,ku,lo,sasad,hh', 'da']

How to get remaining string?

CodePudding user response:

You can do it by splitting on , and joining in chunks:

seq = my_string.split(',')
size = 5
[','.join(seq[pos:pos   size]) for pos in range(0, len(seq), size)]

Output:

['abc,kjj,hg,kj,ls', 'jsh,ku,lo,sasad,hh', 'da']

CodePudding user response:

You do not need regex for this. Just try something like this:

ls = 'abc,kjj,hg,kj,ls,jsh,ku,lo,sasad,hh,da'.split(",")
[",".join(ls[i:i 5]) for i in range(0, len(ls), 5)]

results into this:

['abc,kjj,hg,kj,ls', 'jsh,ku,lo,sasad,hh', 'da']

CodePudding user response:

you can do it this way by counting the , occuerence

>>> my_string = 'abc,kjj,hg,kj,ls,jsh,ku,lo,sasad,hh,da'
>>> start = 0
>>> end = 0
>>> count = 0
>>> 
>>> result = []
>>> for i, v in enumerate(my_string):
...     if v==',':
...             count =1
...     if count ==5:
...             count = 0
...             result.append(my_string[start:i])
...             end = i
...             start = i 1
... 
>>> if end != len(my_string)-1:
...     result.append(my_string[star:])

>>> if end != len(my_string)-1:
...     result.append(my_string[start:])
... 
>>> result
['abc,kjj,hg,kj,ls', 'jsh,ku,lo,sasad,hh', 'da']

CodePudding user response:

str_test = "abc,kjj,hg,kj,ls,jsh,ku,lo,sasad,hh,da"

splitStr = str_test.split(",")
_5str = [",".join(splitStr[i : i   5]) for i in range(0, len(splitStr), 5)]
print(_5str)
>>>['abc,kjj,hg,kj,ls', 'jsh,ku,lo,sasad,hh', 'da']
  • Related