相关疑难解决方法(0)

用逗号分隔并在Python中删除空格

我有一些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,我猜测有更快,更简单,更优雅的方式.

python whitespace strip

312
推荐指数
6
解决办法
48万
查看次数

有没有更好的方法在字符串列表上使用strip()? - 蟒蛇

现在我一直在尝试在字符串列表上执行strip(),我这样做了:

i = 0
for j in alist:
    alist[i] = j.strip()
    i+=1
Run Code Online (Sandbox Code Playgroud)

有没有更好的方法呢?

python string iterator list strip

19
推荐指数
3
解决办法
1万
查看次数

标签 统计

python ×2

strip ×2

iterator ×1

list ×1

string ×1

whitespace ×1