更新时间:2023-02-09 12:25:25
您可以使用列表推导式
my_list = ['this
', 'is
', 'a
', 'list
', 'of
', 'words
']
stripped = [s.strip() for s in my_list]
或者使用 map()
:
stripped = list(map(str.strip, my_list))
在 Python 2 中,map()
直接返回一个列表,因此您不需要调用列表.在 Python 3 中,列表推导式更简洁,通常被认为更惯用.
In Python 2, map()
directly returned a list, so you didn't need the call to list. In Python 3, the list comprehension is more concise and generally considered more idiomatic.