Split Strings into words with multiple word boundary delimiters

前端 未结 30 2630
既然无缘
既然无缘 2020-11-21 05:09

I think what I want to do is a fairly common task but I\'ve found no reference on the web. I have text with punctuation, and I want a list of the words.

\"H         


        
30条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-11-21 05:48

    Heres my take on it....

    def split_string(source,splitlist):
        splits = frozenset(splitlist)
        l = []
        s1 = ""
        for c in source:
            if c in splits:
                if s1:
                    l.append(s1)
                    s1 = ""
            else:
                print s1
                s1 = s1 + c
        if s1:
            l.append(s1)
        return l
    
    >>>out = split_string("First Name,Last Name,Street Address,City,State,Zip Code",",")
    >>>print out
    >>>['First Name', 'Last Name', 'Street Address', 'City', 'State', 'Zip Code']
    

提交回复
热议问题