如何从Python中的列表列表中删除空格?

时间:2017-11-20 05:57:23

标签: python regex

我有以下输入:

[ ['A(x) | B(x)'],['Function(x,y) | K(x)'] ]

列表列表中的每个列表在字符“|”之前和之后都有一个空格。如何删除它以获得以下结果?

[ ['A(x)|B(x)'],['Function(x,y)|K(x)'] ]

我尝试了以下内容:

list=[]
k1=[]
for i in sentence:
    k1="".join(i).replace(" ","")
    list.append(k1)

result=[[i] for i in list]

1 个答案:

答案 0 :(得分:0)

试试这个:

>>> a_list = [['A(x) | B(x)'], ['Function(x,y) | K(x)']]
>>> result = [[''.join(''.join(sub).split())] for sub in a_list]
>>> result
[['A(x)|B(x)'], ['Function(x,y)|K(x)']]