Trying to count words in a string

后端 未结 7 775
醉话见心
醉话见心 2021-02-06 03:28

I\'m trying to analyze the contents of a string. If it has a punctuation mixed in the word I want to replace them with spaces.

For example, If Johnny.Appleseed!is:a*good

7条回答
  •  醉话见心
    2021-02-06 03:54

    Simple loop based solution:

    strs = "Johnny.Appleseed!is:a*good&farmer"
    lis = []
    for c in strs:
        if c.isalnum() or c.isspace():
            lis.append(c)
        else:
            lis.append(' ')
    
    new_strs = "".join(lis)
    print new_strs           #print 'Johnny Appleseed is a good farmer'
    new_strs.split()         #prints ['Johnny', 'Appleseed', 'is', 'a', 'good', 'farmer']
    

    Better solution:

    Using regex:

    >>> import re
    >>> from string import punctuation
    >>> strs = "Johnny.Appleseed!is:a*good&farmer"
    >>> r = re.compile(r'[{}]'.format(punctuation))
    >>> new_strs = r.sub(' ',strs)
    >>> len(new_strs.split())
    6
    #using `re.split`:
    >>> strs = "Johnny.Appleseed!is:a*good&farmer"
    >>> re.split(r'[^0-9A-Za-z]+',strs)
    ['Johnny', 'Appleseed', 'is', 'a', 'good', 'farmer']
    

提交回复
热议问题