栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

利用 python调整字符次序创建新单词 —— 2022/2/14

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

利用 python调整字符次序创建新单词 —— 2022/2/14

创建新单词

具体配置信息请参考网址

首先下载 nltk库

pip install nltk -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

然后下载 nltk_data

git clone http://gitclone.com/github.com/nltk/nltk_data.git

下载完成后进入 nltk_data目录

cd nltk_data

找到其中的 index.xml文件,把文件内所有的

s://raw.githubusercontent.com/nltk/nltk_data/gh-pages

字符串替换成(可以借助 Pycharm的快捷键 ctrl + r)

://localhost:8000

在当前目录下(nltk_data目录下)运行以下命令启动数据提供服务器(不要关闭此终端)

python -m http.server 8000

然后在 Pycharm中编写一个 Python文件

import nltk
nltk.download()

然后在 Pycharm中编写一个 Python文件,运行后得到一个窗口,将窗口中的

https://raw.githubusercontent.com/nltk/nltk_data/gh-pages/index.html

替换成

http://localhost:8000/index.xml

创建新单词

from itertools import permutations
from nltk.corpus import words

def create_new_word():
    word = input("Enter your word:")
    """
    permutations(word) 返回一个可迭代的元组对象
        - 此处将字符串 word中的字符次序打乱,打乱字符的顺序生成一个可迭代的元组对象
            如将:'hi' -->  --> ('h', 'i') ('i', 'h')
        - 然后借助字符串的 join拼接方法进行无元素拼接
            如将:('h', 'i') ('i', 'h') --> 'hi' and 'ih'
    """
    new_word_list = [''.join(data) for data in permutations(word)]
    for new_word in new_word_list:
        if new_word in words.words():  # words.words() 为可迭代列表
            print(new_word)


create_new_word()

细学 itertools 库

def study_itertools():
    import itertools  # itertools 迭代工具
    """
    1. itertools.count(start, step)
        - 默认返回一个从0开始,依次+1的自然数迭代器
        - 可以用 start指定开始的位置,step指定每次迭代时的间隔(步长)
    """
    for i in itertools.count(start=0, step=1):
        print(i, end=' ')
        if i == 10:
            break
    print()  # 0 1 2 3 4 5 6 7 8 9 10 

    """
    2. itertools.cycle()
        - 无限迭代传入的可迭代对象
    """
    # my_list = [1, 2, 3]
    # for i in itertools.cycle(my_list):
    #     print(i, end=' ')
    # print()

    """
    3. itertools.repeat(object, times=None)
        - 无限迭代传入的对象
    """
    # my_tuple = (1, 2, 3)
    # for i in itertools.repeat(my_tuple, times=3):
    #     print(i, end=' ')
    # print()  # (1, 2, 3) (1, 2, 3) (1, 2, 3)

    """
    4. itertools.accumulate(iterable, func)
        - iterable 指传入一个可迭代的对象
        - func 代表一个函数,对取到的数据进行累积计算并返回(默认是累加可迭代对象中的元素)
            默认为:add()
    """
    # def multiply(num_1, num_2):
    #     return num_1 * num_2
    #
    # def add(num_1, num_2):
    #     return num_1 + num_2
    #
    # my_list = [3, 6, 9, 12, 15]
    # for i in itertools.accumulate(my_list, func=multiply):
    #     print(i, end=' ')
    # print()  # 3 18 162 1944 29160

    """
    5. itertools.chain()
        - 可以传入任意数量的序列,并将里面的元素全部遍历出来
    """
    # my_list = [1, 2, 3]
    # my_tuple = (6, 7, 8)
    # my_str = 'love'
    # for i in itertools.chain(my_list, my_tuple, my_str):
    #     print(i, end=' ')
    # print()  # 1 2 3 6 7 8 l o v e


study_itertools()
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/739462.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号