有几种方法可以去除字符串中的空字符串:
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']
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:mysql中怎么去除空字符串