第1个小脚本:英语单语按字母个数进行升序排序

单词来源于coca单词库

效果如下:

2个字母的单词

3个字母的单词

4个字母的单词

5个字母的单词

...

n个字母的单词

bash 复制代码
def sort_words_by_length_and_alphabet(text):
    # 将输入文本按空格分割为单词列表
    words = text.split()

    # 去除标点符号
    import string
    words = [word.translate(str.maketrans('', '', string.punctuation)) for word in words]

    # 去掉重复的单词
    unique_words = set(words)
    #将所有单词转换为小写字母
    unique_words = [word.lower() for word in unique_words]

    # 按照每个单词的长度和字母顺序进行排序
    sorted_words = sorted(unique_words, key=lambda word: (len(word), word))

    # 返回排序后的单词列表
    return sorted_words


# 示例用法
if __name__ == "__main__":
    # 读取文件内容
    with open('COCA_20000.txt', 'r', encoding='utf-8') as file:
        text = file.read()

    # 提取单词并排序
    sorted_words = sort_words_by_length_and_alphabet(text)
    print("按单词长度和字母顺序升序排序的结果:", sorted_words)

    # 保存到文件
    with open('sorted_words.txt', 'w', encoding='utf-8') as file:
        for word in sorted_words:
            file.write(word + '\n')
    print("已保存到文件 sorted_words.txt")
相关推荐
机器学习之心26 分钟前
小波增强型KAN网络 + SHAP可解释性分析(Pytorch实现)
人工智能·pytorch·python·kan网络
JavaEdge在掘金32 分钟前
MySQL 8.0 的隐藏索引:索引管理的利器,还是性能陷阱?
python
站大爷IP43 分钟前
Python办公自动化实战:手把手教你打造智能邮件发送工具
python
chao_7891 小时前
回溯题解——子集【LeetCode】二进制枚举法
开发语言·数据结构·python·算法·leetcode
zdw1 小时前
fit parse解析佳明.fit 运动数据,模仿zwift数据展示
python
剑桥折刀s2 小时前
Python打卡:Day46
python
巴里巴气2 小时前
Python爬虫图片验证码和滑块验证码识别总结
爬虫·python
sword devil9003 小时前
PYQT实战:智能家居中控
python·智能家居·pyqt
NetX行者3 小时前
FastMCP:用于构建MCP服务器的开源Python框架
服务器·python·开源
超龄超能程序猿3 小时前
(3)机器学习小白入门 YOLOv: 解锁图片分类新技能
python·numpy·pandas·scipy