Reputation: 35
I'm a beginner in Python and coding in general, so I feel like I'm out of my depth with this problem. I'm trying to make a list out of a string but with some modifications.
Let's say I have some code like this:
input_text = "gʷʰsoʷngʲʷʰos"
modifiers_list = ["ʷ", "ʰ", "ʲ"]
I would like to be able to turn the input text into a list of characters, but with modifiers being a part of the preceding characters, e.g.:
output_list = ['gʷʰ', 's', 'oʷ', 'n', 'gʲʷʰ', 'o', 's']
I've been trying to come up with a solution for a few days now, looking through numerous SO threads but with no success.
Upvotes: 1
Views: 70
Reputation: 43166
You could use regex:
import re
modifiers = ''.join(map(re.escape, modifiers_list))
pattern = r'\w(?:[{}])*'.format(modifiers)
output_list = re.findall(pattern, input_text)
print(output_list) # ['gʷʰ', 's', 'oʷ', 'n', 'gʲʷʰ', 'o', 's']
The pattern that's constructed looks like \w(?:[ʷʰʲ])*
, and it matches a single character followed by any number of modifiers.
Upvotes: 1