import re

def count_words(string):
    '统计字符串中每个英文单词出现的次数,并以列表形式输出'
    # 将字符串转为小写,方便比较
    string = string.lower()
    # 提取单词,使用正则表达式匹配
    words = re.findall(r'\b\w+\b', string)
    # 统计每个单词的出现次数
    word_count = {}
    for word in words:
        if word not in word_count:
            word_count[word] = 1
        else:
            word_count[word] += 1
    # 将结果转为列表并按出现次数从大到小排序
    result = sorted(word_count.items(), key=lambda x: x[1], reverse=True)
    return result

# 测试
string = 'This is a test string. This string is used for testing the count_words function.'
print(count_words(string))
# 输出:[('this', 2), ('is', 2), ('string', 2), ('a', 1), ('test', 1), ('used', 1), ('for', 1), ('testing', 1), ('the', 1), ('count_words', 1), ('function', 1)]
Python 字符串单词计数:统计英文单词出现次数并排序输出

原文地址: https://www.cveoy.top/t/topic/opDL 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录