import itertools
first = 'f' #look for words starting with f letter
nested_list = [['This is first sentence'],['This is following sentence']]
flat_list = list(itertools.chain.from_iterable(nested_list))
nested_words = [i.split(' ') for i in flat_list]
words = list(itertools.chain.from_iterable(nested_words))
lst = [i for i in words if i[0]==first]
print(lst) #output: ['first', 'following']
如果我错了,你只需要一个包含所有以特定字符开头的单词的列表。为此,我将使用列表展平(通过
itertools
):相关问题 更多 >
编程相关推荐