拆分多种分隔符的字符串

map实现

def mySplit(s, ds):
    res = [s]

    for d in ds:
        t = []
        list(map(lambda x: t.extend(x.split(d)), res))
        res = t
    return [x for x in res if x]


s = 'ab;cd|efg|hi,jkl|mn\topq;rst,uvw\txyz'
ds = ';,|\t'
print(mySplit(s, ds))

re.split实现

import re

s = 'ab;cd|efg|hi,jkl|mn\topq;rst,uvw\txyz'
ds = ';,|\t'


print(re.split(r'[,;\t|]+', s))

猜你喜欢

转载自www.cnblogs.com/ray-mmss/p/10464590.html