第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")
相关推荐
Kyln.Wu30 分钟前
【python实用小脚本-211】[硬件互联] 桌面壁纸×Python梦幻联动|用10行代码实现“开机盲盒”自动化改造实录(建议收藏)
开发语言·python·自动化
Ms_Big1 小时前
ppliteseg改rknn,部署在嵌入式板,加速模型
人工智能·python·深度学习
折翼的恶魔2 小时前
数据分析:合并
python·数据分析·pandas
百锦再2 小时前
在 CentOS 系统上实现定时执行 Python 邮件发送任务
java·linux·开发语言·人工智能·python·centos·pygame
I'm a winner2 小时前
第五章:Python 数据结构:列表、元组与字典(二)
数据结构·python
番薯大佬3 小时前
Python学习-day8 元组tuple
java·python·学习
小文数模3 小时前
2025高教社国赛数学建模C题参考论文(含模型和代码)
python·数学建模·matlab
鸡哥爱技术4 小时前
Django入门笔记
笔记·python·django
猫耳君4 小时前
汽车网络安全 CyberSecurity ISO/SAE 21434 测试之一
python·安全·网络安全·汽车·iso/sae 21434·cybersecurity
勘察加熊人5 小时前
python将pdf转txt,并切割ai
数据库·python·pdf