Home>

Let's imagine that we are given a one-dimensional list of strings.

list_first= ['A man in man', 'Dog is an animal', 'I wasn't there yesterday']

From it I want to get a list of lists like this:

list_second= [['a', 'man', 'in', 'man'], ['dog', 'is', 'an', 'animal'], ['i', 'wasn' , 't', 'there', 'yesterday']

What needs to be done for this? How many times I try -problems with data types

Please correct the question to reflect the specific problem with enough detail to be able to provide an adequate answer. Add your best try code.

A_Vaclav2021-11-19 21:19:29
  • Answer # 1

    the simplest solution is in the forehead:

    list_first= ['A man in man', 'Dog is an animal', 'I wasn \' t there yesterday ']
    list_second= [text.split () for text in list_first]
    

    true, this method has a drawback -it cuts only spaces, and you also have'need to cut off

    The following error pops up: 'list' object has no attribute 'split'

    Jonathan Davis2021-11-19 21:08:01

    strange, everything works for me, show your code plz

    Zhihar2021-11-19 21:10:25

    file_obj= open ('sentences.txt') # some file with sentences text1= file_obj.read () text2= text1.lower () text_for_5= text2.split ('\ n') text_for_5.remove ('') list_second= [ text.split () for text in text_for_5]

    Jonathan Davis2021-11-19 21:16:12

    and what is in text_for_5?

    Zhihar2021-11-19 21:18:45

    List of sentence strings. I also specially punched the text_for_5 type. class 'list', everything is fine with the type

    Jonathan Davis2021-11-19 21:21:12
  • Answer # 2

    the simplest solution is in the forehead:

    list_first= ['A man in man', 'Dog is an animal', 'I wasn \' t there yesterday ']
    list_second= [text.split () for text in list_first]
    

    true, this method has a drawback -it cuts only spaces, and you also have'need to cut off

    The following error pops up: 'list' object has no attribute 'split'

    Jonathan Davis2021-11-19 21:08:01

    strange, everything works for me, show your code plz

    Zhihar2021-11-19 21:10:25

    file_obj= open ('sentences.txt') # some file with sentences text1= file_obj.read () text2= text1.lower () text_for_5= text2.split ('\ n') text_for_5.remove ('') list_second= [ text.split () for text in text_for_5]

    Jonathan Davis2021-11-19 21:16:12

    and what is in text_for_5?

    Zhihar2021-11-19 21:18:45

    List of sentence strings. I also specially punched the text_for_5 type. class 'list', everything is fine with the type

    Jonathan Davis2021-11-19 21:21:12