在理解中,嵌套列表迭代应遵循与等效嵌套for循环相同的顺序。
为了理解这个问题,我们将从NLP中选取一个简单的例子。您想要创建一个单词列表,其中包含来自句子列表的所有单词,其中每个句子都是一个单词列表。
>>> list_of_sentences = [['The','cat','chases', 'the', 'mouse','.'],['The','dog','barks','.']]
>>> all_words = [word for sentence in list_of_sentences for word in sentence]
>>> all_words
['The', 'cat', 'chases', 'the', 'mouse', '.', 'The', 'dog', 'barks', '.']
为了消除重复的单词,你可以使用集合 {} 而不是列表 []。
>>> all_unique_words = list({word for sentence in list_of_sentences for word in sentence}]
>>> all_unique_words
['.', 'dog', 'the', 'chase', 'barks', 'mouse', 'The', 'cat']
或者应用list(set(all_words))
>>> all_unique_words = list(set(all_words))
['.', 'dog', 'the', 'chases', 'barks', 'mouse', 'The', 'cat']
itertools.chain
:list(chain.from_iterable(entry for tag in tags for entry in entries if tag in entry))
- Ashwini Chaudhary