我需要打印列表值的唯一单词

I need to print unique words of the value of list

我的变量列表中有 800 多个单词 words。我按单词长度将前 100 个单词拆分并排序为 list_one,其余部分在 list_two

我得到的 list_one 是..

list_one = ['1', 'a', 'a', 'a', 'a', 'a', 'a', 'it', 'is', 'in', 'of', 'be', 'in', 'of', 'or', 'of', 'be', 'on', 'is', 'so', 'in', 'of', 'he', 'is', 'of', 'or', 'of', 'my', 'mr', 'and', 'set', 'url', 'org', 'txt', 'man', 'the', 'man', 'may', 'his', 'the', 'the', 'the', 'one', 'his', 'jane', '1342', 'that', 'good', 'must', 'want', 'wife', 'such', 'this', 'well', 'that', 'some', 'dear', 'said', 'lady', 'title', 'pride', 'utf-8', 'https', 'files', 'truth', 'known', 'views', 'first', 'truth', 'fixed', 'minds', 'other', 'their', 'author', 'austen', '1342-0', 'single', 'little', 'bennet', 'english', 'chapter', 'fortune', 'however', 'language', 'encoding', 'feelings', 'entering', 'families', 'rightful', 'property', 'prejudice', 'character', 'gutenberg', 'daughters', 'possession', 'considered', 'universally', 'surrounding', 'acknowledged', 'neighbourhood']

现在我必须将 list_one 的列表更改为 set。我所做的是:

print(set(list_one))

但是,它显示了 list_onelist_two 混合的随机单词输出。我该如何解决这个问题?我不明白为什么我没有得到 list_one.

的唯一词

它应该是这样的:

['a', '1', 'is', 'he', 'be', 'in', 'of', 'or', 'mr', 'my', 'so', 'on', 'it', 'txt', 'set', 'one', 'url', 'and', 'his', 'org', 'man', 'the', 'may', 'jane', 'wife', 'this', '1342', 'want', 'said', 'some', 'that', 'such', 'must', 'lady', 'well', 'good', 'dear', 'pride', 'https', 'known', 'other', 'their', 'title', 'first', 'truth', 'fixed', 'files', 'utf-8', 'minds', 'views', 'little', 'author', 'single', 'bennet', '1342-0', 'austen', 'chapter', 'english', 'however', 'fortune', 'feelings', 'property', 'encoding', 'rightful', 'entering', 'families', 'language', 'prejudice', 'gutenberg', 'daughters', 'character', 'considered', 'possession', 'universally', 'surrounding', 'acknowledged', 'neighbourhood']

set forms unordered collection of unique elements. In order to get ordered collection, sorted 可以使用具有适当 key 参数的函数:

list_one = ['1', 'a', 'a', 'a', 'a', 'a', 'a', 'it', 'is', 'in', 'of', 'be', 'in', 'of', 'or', 'of', 'be', 'on', 'is', 'so', 'in', 'of', 'he', 'is', 'of', 'or', 'of', 'my', 'mr', 'and', 'set', 'url', 'org', 'txt', 'man', 'the', 'man', 'may', 'his', 'the', 'the', 'the', 'one', 'his', 'jane', '1342', 'that', 'good', 'must', 'want', 'wife', 'such', 'this', 'well', 'that', 'some', 'dear', 'said', 'lady', 'title', 'pride', 'utf-8', 'https', 'files', 'truth', 'known', 'views', 'first', 'truth', 'fixed', 'minds', 'other', 'their', 'author', 'austen', '1342-0', 'single', 'little', 'bennet', 'english', 'chapter', 'fortune', 'however', 'language', 'encoding', 'feelings', 'entering', 'families', 'rightful', 'property', 'prejudice', 'character', 'gutenberg', 'daughters', 'possession', 'considered', 'universally', 'surrounding', 'acknowledged', 'neighbourhood']

unique_words = sorted(set(list_one), key=lambda x: len(x))

unique_words 应如下所示:

['a', '1', 'is', 'in', 'so', 'mr', 'on', 'or', 'it', 'of', 'be', 'he', 'my', 'one', 'url', 'org', 'txt', 'the', 'and', 'his', 'man', 'may', 'set', 'well', 'lady', 'wife', 'dear', 'jane', 'that', 'must', 'good', 'said', 'this', 'such', 'some', 'want', '1342', 'files', 'fixed', 'https', 'title', 'known', 'views', 'first', 'other', 'pride', 'minds', 'their', 'truth', 'utf-8', 'bennet', 'single', '1342-0', 'author', 'little', 'austen', 'english', 'chapter', 'fortune', 'however', 'language', 'encoding', 'feelings', 'property', 'rightful', 'entering', 'families', 'character', 'prejudice', 'gutenberg', 'daughters', 'possession', 'considered', 'universally', 'surrounding', 'acknowledged', 'neighbourhood']

设置应该return唯一值。要么是你的代码有问题,要么是你将“list_one”重新分配给两个列表的某种混合,要么是你的拆分出了问题。你能分享整个代码吗?