zara kolagar
zara kolagar

Reputation: 901

python: split a list of strings based on a condition

I have a list like the following:

my_list =['[] [How, you, today] [] []','[] [the] [weather, nice, though] [] []']

I would like to split the list in a way to get the following output.

output_list = ['[]', '[How, you, today]', '[]', '[]','[]', '[the]',
      '[weather, nice, though]', '[]', '[]']

I have tried the following but do not get the result i am looking for.

[word for line in my_list for word in line.split('')]

Upvotes: 0

Views: 89

Answers (2)

dww142
dww142

Reputation: 96

This link is for java, but the regular expression is the same: Split string by all spaces except those in brackets

split by regex in python (add import re to your script):

rx = '\\s+(?![^\\[]*\\])'

# join strings into one string, then split back to list from there
output_list = re.split(rx, ' '.join(my_list))

# ['[]', '[How, you, today]', '[]', '[]', '[]', '[the]', '[weather, nice, though]', '[]', '[]']

Upvotes: 2

ShlomiF
ShlomiF

Reputation: 2895

Almost. First of all, the following produces what you want -

[('[' + word) if i else word for line in my_list for i, word in enumerate(line.split(' ['))]

More specifically, it seems you want to split by a space-character, but only if it's the beginning of a new list of words. Hence splitting with ' ['.
Next up you'll be missing the opening bracket for each list except the first. So by "manually" adding it on for all except the first (by using an enumeration index), you get the requested output.

Upvotes: 1

Related Questions