列表映射到列表
问题描述:
的名单我有这样一个字符串列表:列表映射到列表
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."]]
我怎样才能做到这一点?
答
list_of_strings = ["string one", "string two", "etc."]
list_of_lists = [x.split() for x in list_of_strings]
+0
FWIW,这根本不适用于改写的问题,但它是对原始问题的回答。 –
答
这个怎么样?
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 ]))
但如果我是你,我会写的几行,使之可读。
答
>>> 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.']]
好的,你是Python新手,但我敢打赌,你至少在编码方面尝试过,对吧? – Raptor
你的意思是你想把列表中的每个字符串转换成一个单词列表? –
是的。我仍在寻找解决方案。对不起,如果它是烦你 – Ideal