gpt4 book ai didi

python - 在字符串拆分期间删除空条目

转载 作者:太空狗 更新时间:2023-10-30 01:05:12 25 4
gpt4 key购买 nike

目前我使用这个辅助函数来删除空条目。

有内置方法吗?

def getNonEmptyList(str, splitSym):
lst=str.split(splitSym)

lst1=[]
for entry in lst:
if entry.strip() !='':
lst1.append(entry)

return lst1

最佳答案

str.split(sep=None, maxsplit=-1)

If sep is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace.

例如:

>>> '1 2 3'.split()
['1', '2', '3']
>>> '1 2 3'.split(maxsplit=1)
['1', '2 3']
>>> ' 1 2 3 '.split()
['1', '2', '3']

关于python - 在字符串拆分期间删除空条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46881946/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com