在python 3中,我有一个列表,该列表中的每个元素都是一个句子字符串,例如
list = ["the dog ate a bone", "the cat is fat"] 如何将每个句子串分割成一个单独的列表,同时将所有内容都保留在这个单独的列表中,使其成为一个二维列表
例如..。
list_2 = [["the", "dog", "ate", "a", "bone"], ["the", "cat", "is", "cat"]]发布于 2016-09-12 00:25:23
您可以使用列表理解:
list2 = [s.split(' ') for s in list]发布于 2016-09-11 23:33:28
您可以简单地对每个值执行following.Use split()方法,并使用新的逗号分隔文本重新分配每个索引的值
mylist=['the dog ate a bone', 'the cat is fat']
print(mylist)
def make_two_dimensional(list):
counter=0
for value in list:
list[counter]= value.split(' ')
counter += 1
make_two_dimensional(mylist)
print(mylist)https://stackoverflow.com/questions/39437036
复制相似问题