Discuss / Python / 批量切片

批量切片

Topic source

疯子_20990

#1 Created at ... [Delete] [Delete and Lock User]

def trim(s): if not isinstance(s, str): raise TypeError('bad input type')

# 批量寻找,找到成串的统一处理
cnt = 0
i = 0
string = ""
for n in s:
    if n != ' ':
        cnt += 1
    elif cnt > 0:
        string += s[i - cnt:i]
        cnt = 0
    i += 1
return string

  • 1

Reply