Home > Enterprise >  Python: how do I split() each symbol until reach separator symbol
Python: how do I split() each symbol until reach separator symbol

Time:11-22

I have a string:

a = "12356789:10:51:52:53"

As a result, I need a list like:

['1','2','3','5','6','7','8','9','10','51','52','53']

How can I split one by one symbols until I find : separator?

CodePudding user response:

You can try this:

list((x:="12356789:10:51:52:53".split(":"))[0])   x[1:]

CodePudding user response:

A short tricky way for fun (wouldn't recommend for production code :-):

a = "12356789:10:51:52:53"

[*b], *b[len(a):] = a.split(':')

print(b)

Output:

['1', '2', '3', '5', '6', '7', '8', '9', '10', '51', '52', '53']

CodePudding user response:

Will this do?

a = "12356789:10:51:52:53"

l1 = a.split(":")
l2 = [c for c in l1[0]]
output = l2   l1[1:]
output 

CodePudding user response:

In [193]: a = "12356789:10:51:52:53"

In [194]: first_split = a.index(':')

In [195]: a[:first_split].split()
Out[195]: ['12356789']

In [196]: a = "12356789:10:51:52:53"

In [197]: b = list(a[:first_split])
Out[197]: ['1', '2', '3', '5', '6', '7', '8', '9']

In [198]: c = a[first_split 1:].split(':')
Out[198]: ['10', '51', '52', '53']

Then you can combine b c

CodePudding user response:

Not sure if this is the best solution but this would work:

a = "12356789:10:51:52:53"
list(''.join(a.split(":")[0]))   a.split(":")[1:]
  • Related