【问题标题】:Create a list of alphabetically sorted UNIQUE words and display the first N words in python创建一个按字母顺序排序的 UNIQUE 单词列表并在 python 中显示前 N 个单词
【发布时间】:2020-10-27 15:30:29
【问题描述】:

我是 Python 新手,为一个简单的问题道歉。我的任务如下:

创建按字母顺序排列的唯一单词列表并显示前 5 个单词

我有text变量,里面包含很多文字信息

我做到了

test = text.split()
sorted(test)

因此,我收到了一个列表,该列表以 $ 和数字等符号开头。

如何获取单词并打印 N 个单词。

【问题讨论】:

  • 你把什么定义为word?至于第二个问题,只需使用sorted 分割您创建的列表
  • 您只关心字母单词吗?例如,您是否只想打印仅包含字母的前 5 个字符串而忘记其余的?
  • 一个很好的问题。我在想我需要打印真实的单词,而不仅仅是前 5 个元素。
  • @AnakinSkywalker 看看 python 的内置 filter、列表切片和 regex 库。这应该会让你继续前进
  • “独特的词”到底是什么意思?它是指字面上只在输入列表中出现一次的单词,还是只是不同的单词,无论它们出现的频率如何?

标签: python string sorting alphabetical


【解决方案1】:

您可以将排序后的返回列表切片到第 5 个位置

sorted(test)[:5]

或者如果只寻找单词

sorted([i for i in test if i.isalpha()])[:5]

或通过正则表达式

sorted([i for i in test if re.search(r"[a-zA-Z]")])

通过使用列表的切片,您将能够获取所有列表元素,直到在这种情况下为特定索引 5。

【讨论】:

  • 但 OP 想要获取实际的字母单词,我认为他们不想打印不是单词的元素
  • @Chase,我想是的,我会自己做。但我还是给出了一点。
  • @LeoArad,谢谢!我得到输出:['001','002','002','003','006']。可能是因为它们也是字符串。如何提取字母?任何想法?感谢您的时间!
  • 将其更改为.isalpha(),它将只返回单词
  • 这没有得到 unique 字 - 它只得到前五个字,可能是 ['a', 'a', 'a', 'a', 'a']
【解决方案2】:

我假设“单词”是指仅由字母字符组成的字符串。在这种情况下,您可以使用.filter 首先删除不需要的字符串,将其转换为set,对其进行排序并然后打印您的内容。

text = "$1523-the king of the 521236 mountain rests atop the king mountain's peak $@"
# Extract only the words that consist of alphabets
words = filter(lambda x: x.isalpha(), text.split(' '))
# Print the first 5 words
sorted(set(words))[:5]

输出-

['atop', 'king', 'mountain', 'of', 'peak']

但问题在于它仍然会忽略像mountain's 这样的词,因为那个讨厌的'。在这种情况下,正则表达式解决方案实际上可能要好得多-

现在,我们将使用这个正则表达式 - ^[A-Za-z']+$,这意味着字符串必须只包含字母和',您可以根据您的需要在这个正则表达式中添加更多内容视为“词”。阅读更多关于正则表达式的信息here

这次我们将使用re.match 而不是.isalpha

WORD_PATTERN = re.compile(r"^[A-Za-z']+$")
text = "$1523-the king of the 521236 mountain rests atop the king mountain's peak $@"
# Extract only the words that consist of alphabets
words = filter(lambda x: bool(WORD_PATTERN.match(x)), text.split(' '))
# Print the first 5 words
sorted(set(words))[:5]

输出-

['atop', 'king', 'mountain', "mountain's", 'of']

但是请记住,当您有像 hi! What's your name? 这样的字符串时,这会变得很棘手。 hi!name? 都是单词,但它们不是完全按字母顺序排列的。这样做的诀窍是首先将它们拆分为hi 而不是hi!name 而不是name?

不幸的是,真正的分词远远超出了这个问题的范围。我建议看看this question

【讨论】:

    【解决方案3】:

    我是新手,如有错误请见谅。谢谢。

    test = '''The coronavirus outbreak has hit hard the cattle farmers in Pabna and Sirajganj as they are now getting hardly any customer for the animals they prepared for the last year targeting the Eid-ul-Azha this year.
    
    Normally, cattle traders flock in large numbers to the belt -- one of the biggest cattle producing areas of the country -- one month ahead of the festival, when Muslims slaughter animals as part of their efforts to honour Prophet Ibrahim's spirit of sacrifice.
    
    But the scene is different this year.'''
    
    test = test.lower().split()
    
    test2 = sorted([j for j in test if j.isalpha()])
    
    print(test2[:5])
    

    【讨论】:

      猜你喜欢
      • 2018-07-13
      • 2012-11-28
      • 1970-01-01
      • 2021-05-21
      • 2015-05-20
      • 1970-01-01
      • 2013-09-24
      • 1970-01-01
      相关资源
      最近更新 更多