How to split strings inside a list by whitespace characters

前端 未结 4 1609
走了就别回头了
走了就别回头了 2020-11-30 11:57

So stdin returns a string of text into a list, and multiple lines of text are all list elements. How do you split them all into single words?

mylist = [\'thi         


        
4条回答
  •  死守一世寂寞
    2020-11-30 12:18

    You can use simple list comprehension, like:

    newlist = [word for line in mylist for word in line.split()]

    This generates:

    >>> [word for line in mylist for word in line.split()]
    ['this', 'is', 'a', 'string', 'of', 'text', 'this', 'is', 'a', 'different', 'string', 'of', 'text', 'and', 'for', 'good', 'measure', 'here', 'is', 'another', 'one']
    

提交回复
热议问题