如何按列表中的一个项目来分解列表?

2024-05-29 04:24:56 发布

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

如何按"."字符将此列表分解为两个较小的列表

示例:

word_list = ["I", "love", "icecream", ".", "I", "want", "to", "eat", "chip", "."]

smaller_list_1 = ["I", "love", "icecream", "."]

smaller_list_2 = ["I", "want", "to", "eat", "chip", "."]

Tags: to示例列表字符listwordchipwant
1条回答
网友
1楼 · 发布于 2024-05-29 04:24:56

有一种方法可以用字符串来实现。 代替列表,创建如下字符串:

foo = "I love icecream. I want to eat chip."

然后使用split()函数:

list = foo.split(".")

你会发现:

["I love icecream", "I want to eat chip"]

然后,将两个列表项按空格拆分:

list1 = list[0].split(" ")
list2 = list[1].split(" ")

你会发现:

["I", "love", "iceceam"]
["I", "want", "to", "eat", "chip"]

相关问题 更多 >

    热门问题