Python 官方文档:入门教程 => 点击学习
有几种方法可以去除字符串中的空字符串: 使用循环和条件语句: def remove_empty_strings(strings)
有几种方法可以去除字符串中的空字符串:
def remove_empty_strings(strings):
result = []
for string in strings:
if string != "":
result.append(string)
return result
def remove_empty_strings(strings):
return [string for string in strings if string != ""]
def remove_empty_strings(strings):
return list(filter(lambda string: string != "", strings))
可以通过调用这些函数来去除字符串中的空字符串,例如:
strings = ["hello", "", "world", "", "python"]
result = remove_empty_strings(strings)
print(result)
输出:
['hello', 'world', 'Python']
--结束END--
本文标题: python怎么去除空字符串
本文链接: https://www.lsjlt.com/news/570547.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0