如何从Python数组中删除多余的空格?
我有一个python数组,其中包含一组混合值,看起来有点像下面的示例:如何从Python数组中删除多余的空格?,python,arrays,Python,Arrays,我有一个python数组,其中包含一组混合值,看起来有点像下面的示例: ['0012 ', ' 318422 ', ' www.example.com'] 我的问题是,如何轻松地从数组中的值中删除前导空格和尾随空格,使其看起来像这样: ['0012', '318422', 'www.example.com'] 像这样 [x.strip() for x in mylist] 像这样 [x.strip() for x in mylist] 您可以使用map和strip: a = ['0012
['0012 ', ' 318422 ', ' www.example.com']
我的问题是,如何轻松地从数组中的值中删除前导空格和尾随空格,使其看起来像这样:
['0012', '318422', 'www.example.com']
像这样
[x.strip() for x in mylist]
像这样
[x.strip() for x in mylist]
您可以使用
map
和strip
:
a = ['0012 ', ' 318422 ', ' www.example.com']
clean_a = map(lambda x: x.strip(), a)
您可以使用
map
和strip
:
a = ['0012 ', ' 318422 ', ' www.example.com']
clean_a = map(lambda x: x.strip(), a)