mylist = ['A','12','D']
我也无法应用' .join(mylist)并输入检查列表,因为它全部在str中。
答案 0 :(得分:2)
您可以使用正则表达式:
import re
my_string = ''.join(mylist)
# 'A[BC]D[EFG]'
# Replace '[' or ']' with ',' in my_string
pattern = '[\[\]]'
my_string = re.sub(pattern, ',', my_string)
# 'A,BC,D,EFG,'
my_list = my_string.split(',')
# ['A', 'BC', 'D', 'EFG', '']
new_list = [letter for letter in my_list if letter]
# ['A', 'BC', 'D', 'EFG']
答案 1 :(得分:0)
你可以试试这个:
mylist = ['A','[','B','C',']','D']
final_list = []
temp1_val = ''
flag = False
for i in mylist:
if i == '[':
flag = True
elif i == ']':
final_list.append(temp1_val)
temp1_val = ''
flag = False
elif flag:
temp1_val += i
elif not flag:
final_list.append(i)
输出:
['A', 'BC', 'D']
第二个例子的输出:
['A', 'BC', 'D', 'EFG']