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

标签 python string list iterator strip

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

i = 0
for j in alist:
    alist[i] = j.strip()
    i+=1

有更好的方法吗?

最佳答案

您可能不应该使用 list 作为变量名,因为它是一种类型。无论如何:

list = map(str.strip, list) 

这会将函数 str.strip 应用于 list 中的每个元素,返回一个新列表,并将结果存储回 list .

关于python - 有没有更好的方法在字符串列表上使用 strip() ? - Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12182777/

相关文章:

java - 使用泛型返回列表的接口(interface)会导致警告

java - 以编程方式设置列表标识符

r - R中的向量列表-提取向量的一个元素

python - 在大型 numpy 数组中查找常量子数组

javascript - 用 HTML 元素包裹字符串的特定部分

python - AttributeError: 'module'对象没有属性 'cv'

javascript - Angular2/ typescript : error TS2345: Argument of type 'String' is not assignable to parameter of type 'string'

python - 如何在单独的列表中拆分字符串中的每个单词

python - 在VBA Outlook中运行Python或PowerShell

python - 使用 Inspect 元素进行抓取