【问题标题】:I need to print unique words of the value of list我需要打印列表值的唯一单词
【发布时间】:2021-09-27 08:21:28
【问题描述】:

我的变量列表words 中有超过 800 个单词。我将前 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']

【问题讨论】:

  • 请粘贴您的完整代码。

标签: python list set


【解决方案1】:

set 形成独特元素的无序集合。为了得到有序集合,可以使用带有正确key参数的sorted函数:

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']

【讨论】:

  • 谢谢。它返回的正是我所需要的
【解决方案2】:

Set 应该只返回唯一值。要么您的代码有问题,要么您将“list_one”重新分配给两个列表的混合,或者您的拆分出现问题。你能分享整个代码吗?

【讨论】:

  • 我修复了 Rustam 发布的代码并且它有效
  • 很好,你的问题解决了,但如果你从 list_two 得到消息,我认为前面还有另一个问题
猜你喜欢
  • 2017-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-22
  • 1970-01-01
  • 1970-01-01
  • 2021-01-17
  • 2017-07-02
相关资源
最近更新 更多