There is a similar question already on Stack Overflow see link, but I am having problems referencing the item before i. I'm working with a list of strings, and only need to combine neighboring strings in the list when a certain string starts with specific characters because that string is erroneously dividing the neighboring strings. For example:
list = ["a","b","<s16","c","d"]
In this case I would want to combine any two elements neighboring the string that starts with "<s16"
(starts with because each occurance includes a different number). So the correct list would look like this: list = ["a","bc","d"]
I have tried several methods and the recurring problems are:
i.startswith
does not work with integer objects (when I try to userange(len(list))
for example for the string index)- trying to reference the object before
i
(such as withlist.pop(i-1))
results in type error for an unsupported operand type, I guess because it thinks I'm trying to subtract 1 from a string rather than reference the element before the given element that starts with<s16>
I have tried using re.match
and re.findall
to resolve the first issue, but it does not seem to accruately find the right list items.
if any(re.match('<s16') for i in list):
Thank you in advance for any help and I also apologize in advance for my ignorance, I'm new.