2015-07-22 133 views
-4

的名單我有這樣一個字符串列表:列表映射到列表

mylist = ["This is quite possibly the worst movie ever made. Even my 4 year old hated it and wanted to leave.", "I hate this movie."] 

我想擴大mylist尺寸:

[["This is quite possibly the worst movie ever made. Even my 4 year old hated it and wanted to leave."], ["I hate this movie."]] 

我怎樣才能做到這一點?

+2

好的,你是Python新手,但我敢打賭,你至少在編碼方面嘗試過,對吧? – Raptor

+0

你的意思是你想把列表中的每個字符串轉換成一個單詞列表? –

+0

是的。我仍在尋找解決方案。對不起,如果它是煩你 – Ideal

回答

1
list_of_strings = ["string one", "string two", "etc."] 
list_of_lists = [x.split() for x in list_of_strings] 
+0

FWIW,這根本不適用於改寫的問題,但它是對原始問題的回答。 –

0

這個怎麼樣?

import itertools 
str = ["This is quite possibly the worst movie ever made. Even my 4 year old hated it and wanted to leave.", 'I hate this movie.'] 
result = list(itertools.chain.from_iterable([i.split() for i in str ])) 

但如果我是你,我會寫的幾行,使之可讀。

1
>>> mylist = ["This is quite possibly the worst movie ever made. Even my 4 year old hated it and wanted to leave.", "I hate this movie."] 
>>> [[x] for x in mylist] 
[['This is quite possibly the worst movie ever made. Even my 4 year old hated it and wanted to leave.'], ['I hate this movie.']]