第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")
相关推荐
HyperAI超神经7 分钟前
【vLLM 学习】Prithvi Geospatial Mae
人工智能·python·深度学习·学习·大语言模型·gpu·vllm
逻极8 分钟前
Python MySQL防SQL注入实战:从字符串拼接的坑到参数化查询的救赎
python·mysql·安全·sql注入
赫凯15 分钟前
【强化学习】第一章 强化学习初探
人工智能·python·强化学习
Amewin17 分钟前
window 11 安装pyenv-win管理不同的版本的python
开发语言·python
小鸡吃米…1 小时前
Python编程语言面试问题二
开发语言·python·面试
eve杭1 小时前
AI、大数据与智能时代:从理论基石到实战路径
人工智能·python·5g·网络安全·ai
Honmaple2 小时前
中国四级城市联动数据,包含港澳台,内含json , sql , python 脚本
python·sql·json
BoBoZz192 小时前
Curvatures 曲率的计算、边缘曲率的调整以及曲率、颜色的映射
python·vtk·图形渲染·图形处理
少吃零食多运动2 小时前
【Jupyter notebook修改工作目录】
python·jupyter
Swizard3 小时前
别买树莓派了!3步教你在安卓手机上跑通 CPython + PaddleOCR,打造随身 AI 识别终端
python·ai·移动开发