有没有更好的方法在字符串列表上使用strip()? - 蟒蛇 [英] Is there a better way to use strip() on a list of strings? - python
本文介绍了有没有更好的方法在字符串列表上使用strip()? - 蟒蛇的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
现在我一直在尝试在字符串列表上执行strip(),我这样做了:
For now i've been trying to perform strip() on a list of strings and i did this:
i = 0
for j in alist:
alist[i] = j.strip()
i+=1
有更好的方法吗?
推荐答案
你可能不应该这样做使用 list
作为变量名,因为它是一个类型。无论如何:
You probably shouldn't be using list
as a variable name since it's a type. Regardless:
list = map(str.strip, list)
这将把函数 str.strip
应用于列表中的每个元素
,返回一个新列表,并将结果存回列表
。
This will apply the function str.strip
to every element in list
, return a new list, and store the result back in list
.
这篇关于有没有更好的方法在字符串列表上使用strip()? - 蟒蛇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文