poke_list = [... 'Charizard', '(Mega', 'Charizard', 'X)', '78', '130', ...] #1000+ values
Is it possible to merge strings that start with '('
and end with ')'
and then reinsert it into the same list or a new list?
My desired output poke_list = [... 'Charizard (Mega Charizard X)', '78', '130', ...]
You can iterate over list element checking if each element starts with (
or ends with )
. Once you have find the elements between brackets, you can join them by the string .join
method, like this:
poke_list = ['Charizard', '(Mega', 'Charizard', 'X)', '78', '130']
new_poke_list = []
to_concatenate = []
flag = 0
for item in poke_list:
if item.startswith('(') and not item.endswith(')'):
to_concatenate.append(item)
flag = 1
elif item.endswith(')') and not item.startswith('('):
to_concatenate.append(item)
concatenated = ' '.join(to_concatenate)
new_poke_list.append(concatenated)
to_concatenate = []
flag = 0
elif item.startswith('(') and item.endswith(')'):
new_poke_list.append(item)
else:
if flag == 0:
new_poke_list.append(item)
else:
to_concatenate.append(item)
print(new_poke_list)
The flag
is set to 1
when the element is within brackets, 0
otherwise, so you can manage all cases.
Another way to do it, slightly shorter than other solution
poke_list = ['Bulbasaur', 'Charizard', '(Mega', 'Charizard', 'X)', '78', 'Pikachu', '(Raichu)', '130']
fixed = []
acc = fixed
for x in poke_list:
if x[0] == '(':
acc = [fixed.pop()]
acc.append(x)
if x[-1] == ')':
fixed.append(' '.join(acc))
acc = fixed
if not acc is fixed:
fixed.append(' '.join(acc))
print(fixed)
Also notice that this solution assumes that the broken list doesn't start with a parenthesis to fix, and also manage the case where an item has both opening and closing parenthesis (case excluded in other solution)
The idea is to either append values to main list (fixed) or to some inner list which will be joined later if we have detected opening parenthesis. If the inner list was never closed when exiting the loop (likely illegal) we append it anyway to the fixed list when exiting the loop.
This way of doing things if very similar to the transformation of a flat expression containing parenthesis to a hierarchy of lists. The code would of course be slightly different and should manage more than one level of inner list.
Simple plus-signs can concatenate the strings:
new_charizard = 'Charizard '+'(Mega'+' Charizard '+'X)'
This is a function that merges strings between parenthesis. It assumes that for an open parenthesis there will be a closed one and that parenthesis aren't nested.
def merge_parenthesis(l):
start, length = None, len(l)
i = 0
while i < length:
if start is not None:
l[start] += l[i]
if l[i].endswith(')'):
for _ in range(i-start):
l.pop(start+1)
length = len(l)
start, i = None, start # reset
elif l[i].startswith('('):
start = i
i+=1
It works in place. When it finds an element beginning with an open parenthesis it start appending to this the next elements in the list until it finds the one that ends with a closed parenthesis. Then it removes all the element in between the one with the open parenthesis and the one ending with the closed one.
When the element ending with the closed parenthesis is found the state is restored by setting start to None, adjusting the length variable and setting the i variable in order to continue from the right element.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With