【问题标题】:Regular expressions - counting how many times a word appears in a text正则表达式 - 计算一个单词在文本中出现的次数
【发布时间】:2020-12-25 01:06:05
【问题描述】:

我要设置的是一个函数,给定特定文本将打印出单词['color', 'Colour', 'Color','Colour']appear 的次数。这样我得到以下结果:

assert colorcount("Color Purple") == 1

assert colorcount("Your colour is better than my colour") == 2

assert colorcount("color Color colour Colour") == 4

我拥有的是

import re

def colorcount(text):

all_matches = re.findall('color', 'Colour', 'Color'. 'Colour', text)

return len(all_matches)

print(colorcount(text)

它不起作用,我该如何编写代码以使其按我想要的方式工作?

【问题讨论】:

标签: python regex


【解决方案1】:

您可以简单地将文本转换为特定大小写(即全部小写),然后使用字符串的count() 循环每次出现的关键字:

def colorcount(text):
    KEY_WORDS = [ 'color', 'colour' ]
    counter = 0
    sanitexed_text = text.lower()
    for kw in KEY_WORDS:
        counter += sanitexed_text.count(kw.lower())
    return counter

text = 'color ds das Colour dsafasft e re Color'

# 3
print(colorcount(text))

# All following asserts pass
assert colorcount("Color Purple") == 1
assert colorcount("Your colour is better than my colour") == 2
assert colorcount("color Color colour Colour") == 4

【讨论】:

    【解决方案2】:

    如果你想使用正则表达式,你可以这样做:

    import re
    
    def colorcount(text):
      r = re.compile(r'\bcolour\b | \bcolor\b', flags = re.I | re.X)
      count = len(r.findall(text))
      print(count)
      return count
    
    # These asserts work as expected without raising an AssertionError.
    assert colorcount("Color Purple") == 1
    assert colorcount("Your colour is better than my colour") == 2
    assert colorcount("color Color colour Colour") == 4
    

    哪些输出:

    1
    2
    4
    

    【讨论】:

    • 是否有其他方法不需要我在文本中包含句子?我的意思是说 text = colorcount 里面的文字是什么?
    • 好的!有没有办法在不指定文本的情况下做到这一点?现在您说要搜索哪些句子,但有没有办法在断言中的任何文本中进行代码搜索?
    • @P.ython 返回将值发送到断言,打印返回颜色数给作为用户的您。
    • 哦,是的!正是我的意思,对不起,我没有意识到!非常感谢,以后我遇到 python 问题时,你能提供吗? :)
    • @P.ython 当然可以问,我总是关注新的python问题,当我有空时,我会尽力回答。另附注:(因为 return 是函数到达的最后一条语句,所以在 return 之前打印。)
    【解决方案3】:

    试试这个

    def colorcount(text):
        return len(re.findall('[c|C]olou{0,1}r', text))
    
    assert colorcount("Color Purple") == 1
    assert colorcount("Your colour is better than my colour") == 2
    assert colorcount("color Color colour Colour") == 4
    

    【讨论】:

    • 能否以某种方式完成,以便我只说例如打印 colorcount 内文本的 findall 长度,无论它可能是什么,即在代码中我没有“coloe Purple”或任何东西,但当我运行断言时,它只计算断言中文本所说的颜色?
    • 我不明白你在问什么
    • 我不想让它说 print(colorcount("Color Purple")) #1 print(colorcount("你的颜色比我的颜色好")) #2 print(colorcount("颜色颜色颜色颜色“))我想让它计算在断言中的任何文本中
    • 哦,好的!那么现在我怎样才能让它打印出长度而不指定文本是什么,这样无论断言中的文本是什么,它都会返回数字,例如assert colorcount("color 颜色颜色颜色") == 4
    【解决方案4】:

    使用带有标志re.I(不区分大小写)和re.findll 的正则表达式,然后返回返回列表的长度:

    \bcolou?r\b
    
    import re
    
    def colorcount(text):
      return len(re.findall(r'\bcolou?r\b', text, flags=re.I))
    
    print(colorcount('color Color colour Colour'))
    

    打印:

    4

    【讨论】:

    • 这几乎是我想要的,只是我不希望它只计算文本“颜色颜色颜色颜色”,而是计算颜色计数中给出的任何文本。我该怎么做?
    • 我相信你只是“移动了球门柱”。这根本不是你的问题所说的,你对这个答案的新评论根本不清楚你在寻找什么。您需要使用精确的细节更新您的问题。更新问题后向我发表新评论。
    猜你喜欢
    • 1970-01-01
    • 2015-12-21
    • 2015-04-17
    • 2021-12-31
    • 2017-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多