我有一些python代码分裂逗号,但不剥离空格:
>>> string = "blah, lots , of , spaces, here "
>>> mylist = string.split(',')
>>> print mylist
['blah', ' lots ', ' of ', ' spaces', ' here ']
Run Code Online (Sandbox Code Playgroud)
我宁愿最终删除这样的空格:
['blah', 'lots', 'of', 'spaces', 'here']
Run Code Online (Sandbox Code Playgroud)
我知道我可以遍历列表和strip()每个项目,但是,因为这是Python,我猜测有更快,更简单,更优雅的方式.
现在我一直在尝试在字符串列表上执行strip(),我这样做了:
i = 0
for j in alist:
alist[i] = j.strip()
i+=1
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法呢?