将列表映射到列表列表

2024-05-29 02:12:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这样一个字符串列表:

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."]]

我该怎么做?你知道吗


Tags: theismymoviethisyearoldeven
3条回答

这个怎么样?你知道吗

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 ]))

但如果我是你,我会多写几行让它可读。你知道吗

list_of_strings = ["string one", "string two", "etc."]
list_of_lists = [x.split() for x in list_of_strings]
>>> 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.']]

相关问题 更多 >

    热门问题