How to remove empty string in a list?

前端 未结 9 1652
我在风中等你
我在风中等你 2020-12-28 17:42

For example I have a sentence

\"He is so .... cool!\"

Then I remove all the punctuation and make it in a list.

[\"He\", \"         


        
9条回答
  •  天命终不由人
    2020-12-28 18:33

    You can use a list comprehension:

    cleaned = [x for x in your_list if x]
    

    Although I would use regex to extract the words:

    >>> import re
    >>> sentence = 'This is some cool sentence with,    spaces'
    >>> re.findall(r'(\w+)', sentence)
    ['This', 'is', 'some', 'cool', 'sentence', 'with', 'spaces']
    

提交回复
热议问题