2024-05-29 04:24:56 发布
网友
如何按"."字符将此列表分解为两个较小的列表
"."
示例:
word_list = ["I", "love", "icecream", ".", "I", "want", "to", "eat", "chip", "."] smaller_list_1 = ["I", "love", "icecream", "."] smaller_list_2 = ["I", "want", "to", "eat", "chip", "."]
有一种方法可以用字符串来实现。 代替列表,创建如下字符串:
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"]
有一种方法可以用字符串来实现。 代替列表,创建如下字符串:
然后使用split()函数:
你会发现:
然后,将两个列表项按空格拆分:
你会发现:
相关问题 更多 >
编程相关推荐