在理解中,嵌套列表迭代应该遵循与等效的叠层 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']