Python - splitting a string twice -
I have some data that looks like "string, string, string: other string, other string, other string".
I want to manipulate the first group of "string" at a time if I split the input and confine it on the basis of the colon, then I will end up with a list. Then I can not divide it again because there is no specialty 'division' of the 'list' object. " Alternatively, if I decide to set the boundary based on a comma, then it will return everything (with commas after the comma, which I do not want to manipulate) is only one problem, still with a list, I still can manipulate the first entries using [0], [1] etc. Apart from the fact that the number of "string" always varies, so I'm unable to hardcode the numbers. Any idea to get around this list's range?
Try it out:
import re s = 'string, string , String: Other String, Austestring, Austestring 're.split (r' [,:] ', s) = & gt; ['String,' 'String', 'String', 'Other String', 'Other String', 'Other String'] We use the method of dividing regular expressions and strings Using multiple delimiters, or if you want to manipulate the first group of the string from the second group in a different way, we can create two lists in each list in each group:
[x.split (',') S.split for x (':')] => [['String', 'string', 'string'], ['other string', 'other string', 'other string']] one ?? | Or if you just want to retrieve the string in the first group, just do this:
s.split (':') [0] .split (',') => ['String', 'string', 'string']
Comments
Post a Comment