0

我目前有一个列表列表:

[['Hi my name is'],['What are you doing today'],['Would love some help']]

我想拆分列表中的字符串,同时保留在当前位置。例如

[['Hi','my','name','is']...].. 

我怎样才能做到这一点?

另外,如果我想在搜索后使用特定的列表,比如说我搜索“Doing”,然后想在该特定列表中附加一些东西..我将如何去做呢?

4

3 回答 3

4

您可以使用列表推导来创建新的列表列表,其中所有句子都被拆分:

[lst[0].split() for lst in list_of_lists]

现在您可以遍历它并找到匹配条件的列表:

for sublist in list_of_lists:
    if 'doing' in sublist:
        sublist.append('something')

或不区分大小写,使用any()和生成器表达式;这将是找到匹配项的最少单词数:

for sublist in list_of_lists:
    if any(w.lower() == 'doing' for w in sublist):
        sublist.append('something')
于 2013-11-12T11:27:59.283 回答
1
list1 = [['Hi my name is'],['What are you doing today'],['Would love some help']]

利用

[i[0].split() for i in list1]

然后你会得到像

[['Hi', 'my', 'name', 'is'], ['What', 'are', 'you', 'doing', 'today'], ['Would', 'love', 'some', 'help']]
于 2013-11-12T11:41:04.477 回答
0
l = [['Hi my name is'],['What are you doing today'],['Would love some help']]

for x in l:
    l[l.index(x)] = x[0].split(' ')

print l

或者简单地说:

l = [x[0].split(' ') for x in l]

输出

[['Hi', 'my', 'name', 'is'], ['What', 'are', 'you', 'doing', 'today'], ['Would', 'love', 'some', 'help']]
于 2013-11-12T11:28:53.847 回答