有几种方法可以去除字符串中的空字符串:
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']
以上就是python怎么去除空字符串的详细内容,更多请关注php中文网其它相关文章!
python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号