有幾種方法可以去除字串中的空字串:
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中文網其他相關文章!