如何在字符串列表中的括号之间连接字符串
How to join strings between parentheses in a list of strings
poke_list = [... 'Charizard', '(Mega', 'Charizard', 'X)', '78', '130', ...] #1000+ values
是否可以合并以 '('
开头并以 ')'
结尾的字符串,然后将其重新插入到同一列表或新列表中?
我想要的输出poke_list = [... 'Charizard (Mega Charizard X)', '78', '130', ...]
简单的加号可以连接字符串:
new_charizard = 'Charizard '+'(Mega'+' Charizard '+'X)'
您可以遍历列表元素,检查每个元素是否以 (
开头或以 )
结尾。一旦找到括号之间的元素,就可以通过字符串 .join
方法连接它们,如下所示:
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)
当元素在括号内时,flag
设置为 1
,否则 0
,因此您可以管理所有情况。
另一种方法,比其他解决方案略短
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)
另请注意,此解决方案假定损坏的列表不以要修复的括号开头,并且还管理项目同时具有左括号和右括号的情况(其他解决方案中排除的情况)
我们的想法是将值附加到主列表(固定)或附加到某个内部列表,如果我们检测到左括号,稍后将加入这些内部列表。如果退出循环时内部列表从未关闭(可能是非法的),我们会在退出循环时将它追加到固定列表中。
这种处理方式非常类似于将包含括号的平面表达式转换为列表层次结构。代码当然会略有不同,应该管理不止一层的内部列表。
这是一个在括号之间合并字符串的函数。它假定对于左括号会有一个右括号并且括号没有嵌套。
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
它工作到位。当它找到一个以左括号开头的元素时,它开始将列表中的下一个元素附加到该元素,直到找到以右括号结尾的元素。然后它删除带有左括号的元素和以右括号结尾的元素之间的所有元素。
当找到以右括号结尾的元素时,通过将 start 设置为 None 来恢复状态,调整长度变量并设置 i 变量以便从正确的元素继续。
poke_list = [... 'Charizard', '(Mega', 'Charizard', 'X)', '78', '130', ...] #1000+ values
是否可以合并以 '('
开头并以 ')'
结尾的字符串,然后将其重新插入到同一列表或新列表中?
我想要的输出poke_list = [... 'Charizard (Mega Charizard X)', '78', '130', ...]
简单的加号可以连接字符串:
new_charizard = 'Charizard '+'(Mega'+' Charizard '+'X)'
您可以遍历列表元素,检查每个元素是否以 (
开头或以 )
结尾。一旦找到括号之间的元素,就可以通过字符串 .join
方法连接它们,如下所示:
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)
当元素在括号内时,flag
设置为 1
,否则 0
,因此您可以管理所有情况。
另一种方法,比其他解决方案略短
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)
另请注意,此解决方案假定损坏的列表不以要修复的括号开头,并且还管理项目同时具有左括号和右括号的情况(其他解决方案中排除的情况)
我们的想法是将值附加到主列表(固定)或附加到某个内部列表,如果我们检测到左括号,稍后将加入这些内部列表。如果退出循环时内部列表从未关闭(可能是非法的),我们会在退出循环时将它追加到固定列表中。
这种处理方式非常类似于将包含括号的平面表达式转换为列表层次结构。代码当然会略有不同,应该管理不止一层的内部列表。
这是一个在括号之间合并字符串的函数。它假定对于左括号会有一个右括号并且括号没有嵌套。
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
它工作到位。当它找到一个以左括号开头的元素时,它开始将列表中的下一个元素附加到该元素,直到找到以右括号结尾的元素。然后它删除带有左括号的元素和以右括号结尾的元素之间的所有元素。
当找到以右括号结尾的元素时,通过将 start 设置为 None 来恢复状态,调整长度变量并设置 i 变量以便从正确的元素继续。