在列表列表中分割字符串 [英] Split strings in a list of lists
问题描述
我目前有一个列表列表:
I currently have a list of lists:
[['Hi my name is'],['What are you doing today'],['Would love some help']]
我想在列表中拆分字符串,同时将其保留在当前位置.例如
And I would like to split the strings in the lists, while remaining in their current location. For example
[['Hi','my','name','is']...]..
我该怎么做?
另外,如果我想在搜索特定列表后使用它,说我搜索正在执行",然后想在该特定列表中添加一些内容.我该怎么做呢?/p>
Also, if I would like to use a specific of the lists after searching for it, say I search for "Doing", and then want to append something to that specific list.. how would I go about doing that?
推荐答案
您可以使用列表推导来创建包含所有句子的新列表列表:
You can use a list comprehension to create new list of lists with all the sentences split:
[lst[0].split() for lst in list_of_lists]
现在,您可以循环浏览并找到符合条件的列表:
Now you can loop through this and find the list matching a condition:
for sublist in list_of_lists:
if 'doing' in sublist:
sublist.append('something')
或不区分大小写地搜索,请使用 any()
和生成器表达式;这将是找到匹配项的最少单词数:
or searching case insensitively, use any()
and a generator expression; this will the minimum number of words to find a match:
for sublist in list_of_lists:
if any(w.lower() == 'doing' for w in sublist):
sublist.append('something')
这篇关于在列表列表中分割字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!