Python 通过随机字符生成生成随机名称太慢

Python 通过随机字符生成生成随机名称太慢,python,python-3.x,Python,Python 3.x,我制作了一个程序,一个字母一个字母地创建随机单词来组成随机名称,但是这个程序运行得非常慢。它生成随机字符串,检查它们是否为有效名称,然后检查它们是否与用户给定的名称匹配 我试过Cython,但我注意到Cython只支持Python2.x。我使用Python3.x 代码如下: import sys from random import randint from datetime import datetime lc_alphabet = "bcdfghjklmnpqrstvwxyz" uc_al

我制作了一个程序,一个字母一个字母地创建随机单词来组成随机名称,但是这个程序运行得非常慢。它生成随机字符串,检查它们是否为有效名称,然后检查它们是否与用户给定的名称匹配

我试过Cython,但我注意到Cython只支持Python2.x。我使用Python3.x

代码如下:

import sys
from random import randint
from datetime import datetime

lc_alphabet = "bcdfghjklmnpqrstvwxyz"
uc_alphabet = "BCDFGHJKLMNPQRSTVWXYZ"
lc_vocal = "aeiou"
uc_vocal = "AEIOU"
univ_list = []
verbose = False


def generator():
    name_words = randint(4, 8)
    finished_name = ""
    for i in range(name_words):
        io = randint(0, 3)
        lc_alp_rand = randint(0, 20)
        lc_vcl_rand = randint(0, 4)
        lc_alp_i = lc_alphabet[lc_alp_rand]
        lc_vcl_i = lc_vocal[lc_vcl_rand]
        if io == 0 or io == 1:
            finished_name += lc_alp_i
        if io == 2 or io == 3:
            finished_name += lc_vcl_i
    return finished_name


def name_filtering():
        while True:
            this_name = generator()
            VOCAL_WRONG = False
            CONSONANT_WRONG = False
            univ_wrong_point = 0
            wrong_point = 0
            for y in this_name:
                if y in lc_vocal:
                    wrong_point += 1
                if wrong_point >= len(this_name):
                    univ_wrong_point += 1
                    if verbose is True:
                        print("ALL VOCAL DETECTED " + this_name)
                        VOCAL_WRONG = True

            wrong_point_2 = 0
            for z in range(len(this_name)):
                if this_name[z] in lc_alphabet:
                    wrong_point_2 += 1
                if wrong_point_2 == len(this_name):
                    univ_wrong_point += 1
                    if verbose is True:
                        print("ALL CONSONANT DETECTED " + this_name)
                        CONSONANT_WRONG = True

            for v in range(len(this_name)-1):
                if this_name[v] in lc_vocal and this_name[v+1] in lc_vocal:
                    univ_wrong_point += 1
                    if verbose is True and VOCAL_WRONG is False:
                        print("VOCAL SIDE BY SIDE DETECTED " + this_name)
                        VOCAL_WRONG = True

            if this_name[0] in lc_alphabet and this_name[1] in lc_alphabet and this_name[2] in lc_alphabet:
                univ_wrong_point += 1
                if verbose is True and CONSONANT_WRONG is False:
                    print("3 CONSONANT SIDE BY SIDE DETECTED " + this_name)
                    CONSONANT_WRONG = True

            if this_name[1] in lc_alphabet and this_name[2] in lc_alphabet and this_name[3] in lc_alphabet:
                univ_wrong_point += 1
                if verbose is True and CONSONANT_WRONG is False:
                    print("3 CONSONANT SIDE BY SIDE DETECTED " + this_name)
                    CONSONANT_WRONG = True

            if this_name[0] in lc_alphabet and this_name[1] in lc_alphabet and this_name[2] in lc_alphabet and \
               this_name[3] in lc_alphabet:
                univ_wrong_point += 1
                if verbose is True and CONSONANT_WRONG is False:
                    print("4 CONSONANT SIDE BY SIDE DETECTED " + this_name)
                    CONSONANT_WRONG = True

            if len(this_name) > 5:
                if this_name[2] in lc_alphabet and this_name[3] in lc_alphabet and this_name[4] in lc_alphabet and \
                   this_name[5] in lc_alphabet:
                    univ_wrong_point += 1
                    if verbose is True and CONSONANT_WRONG is False:
                        print("4 CONSONANT SIDE BY SIDE DETECTED " + this_name)
                        CONSONANT_WRONG = True

            if univ_wrong_point == 0:
                return this_name


def search_name(txt):
    disDate = str(datetime.now())
    counter = 0
    number = 0
    while True:
        name = name_filtering()
        name_length = len(name)
        std_space = 10
        fin_space = std_space - name_length
        the_space = " " * fin_space
        if counter == 0:
            print(str(number) + "| ", end="")
        print(name + the_space, end="")
        counter += 1
        if counter == 10:
            print()
            counter = 0
            number += 1
        if name == txt:
            print()
            print()
            print("Name " + txt + " FOUNDED on Number " + str(number))
            print(disDate)
            print(str(datetime.now()))
            break
        sys.stdout.flush()


def check_name(this_name):
    univ_wrong_point = 0
    wrong_point = 0
    for y in this_name:
        if y in lc_vocal:
            wrong_point += 1
        if wrong_point >= len(this_name):
            univ_wrong_point += 1
            if verbose is True:
                print("ALL VOCAL DETECTED " + this_name)

    wrong_point_2 = 0
    for z in range(len(this_name)):
        if this_name[z] in lc_alphabet:
            wrong_point_2 += 1
        if wrong_point_2 == len(this_name):
            univ_wrong_point += 1
            if verbose is True:
                print("ALL CONSONANT DETECTED " + this_name)

    for v in range(len(this_name) - 1):
        if this_name[v] in lc_vocal and this_name[v + 1] in lc_vocal:
            univ_wrong_point += 1
            if verbose is True:
                print("VOCAL SIDE BY SIDE DETECTED " + this_name)

    if this_name[0] in lc_alphabet and this_name[1] in lc_alphabet and this_name[2] in lc_alphabet:
        univ_wrong_point += 1
        if verbose is True:
            print("3 CONSONANT SIDE BY SIDE DETECTED " + this_name)

    if this_name[1] in lc_alphabet and this_name[2] in lc_alphabet and this_name[3] in lc_alphabet:
        univ_wrong_point += 1
        if verbose is True:
            print("3 CONSONANT SIDE BY SIDE DETECTED " + this_name)

    if this_name[0] in lc_alphabet and this_name[1] in lc_alphabet and this_name[2] in lc_alphabet and \
                    this_name[3] in lc_alphabet:
        univ_wrong_point += 1
        if verbose is True:
            print("4 CONSONANT SIDE BY SIDE DETECTED " + this_name)

    if len(this_name) > 5:
        if this_name[2] in lc_alphabet and this_name[3] in lc_alphabet and this_name[4] in lc_alphabet and \
                        this_name[5] in lc_alphabet:
            univ_wrong_point += 1
            if verbose is True:
                print("4 CONSONANT SIDE BY SIDE DETECTED " + this_name)

    if len(this_name) > 8:
        print("TOO LONG (more than 8 letter)")
        univ_wrong_point += 1

    if len(this_name) < 4:
        print("TOO FEW (fewer than 4 letter)")
        univ_wrong_point += 1

    if univ_wrong_point == 0:
        print("this name match criteria")
    else:
        print("this name does not match criteria")

check_name(str(input("Check Name Criteria : ")))
search_name(str(input("Search Name in 4 words : ")))
导入系统 从随机导入randint 从日期时间导入日期时间 lc_alphabet=“bcdfghjklmnpnpqrstvwxyz” uc_alphabet=“bcdfghjklmnpnpqrstvwxyz” lc_vocal=“aeiou” uc_vocal=“AEIOU” 大学列表=[] 冗长=错误 def生成器(): name_words=randint(4,8) 已完成\u name=“” 对于范围内的i(名称和单词): io=randint(0,3) lc_alp_rand=randint(0,20) lc_vcl_rand=randint(0,4) lc_alp_i=lc_字母表[lc_alp_rand] lc_vcl_i=lc_vocal[lc_vcl_rand] 如果io==0或io==1: 已完成\u name+=lc\u alp\u i 如果io==2或io==3: 已完成\u name+=lc\u vcl\u i 返回完成的名称 def name_filtering(): 尽管如此: 此名称=生成器() 发音错误=错误 辅音_错误=错误 大学错误点=0 错误的_点=0 对于使用此名称的y: 如果在lc_中为y: 错误的_点+=1 如果错误的\u点>=len(此\u名称): 大学错误点+=1 如果verbose为True: 打印(“检测到所有声音”+此名称) 错误=正确 错误的\u点\u 2=0 对于范围内的z(len(此名称)): 如果此名称[z]以lc字母表示: 错误的_点_2+=1 如果错误的\u点\u 2==len(此\u名称): 大学错误点+=1 如果verbose为True: 打印(“检测到所有辅音”+此名称) 辅音_错误=正确 对于范围内的v(len(此名称)-1): 如果lc_vocal中的此_名称[v]和lc_vocal中的此_名称[v+1]: 大学错误点+=1 如果verbose为True,而voice\u-False为False: 打印(“检测到声音并排”+此名称) 错误=正确 如果lc_字母表中的_名称[0]和lc_字母表中的_名称[1]以及lc_字母表中的_名称[2]: 大学错误点+=1 如果verbose为真,而辅音_错为假: 打印(“3个辅音并排检测”+此名称) 辅音_错误=正确 如果lc_字母表中的_名称[1]和lc_字母表中的_名称[2]以及lc_字母表中的_名称[3]: 大学错误点+=1 如果verbose为真,而辅音_错为假: 打印(“3个辅音并排检测”+此名称) 辅音_错误=正确 如果lc_字母表中的此_名称[0]和lc_字母表中的此_名称[1]以及lc_字母表中的此_名称[2],并且\ 此字母表中的字母名称[3]: 大学错误点+=1 如果verbose为真,而辅音_错为假: 打印(“4个辅音并排检测”+此名称) 辅音_错误=正确 如果len(此名称)>5: 如果lc_字母表中的_名称[2]和lc_字母表中的_名称[3]以及lc_字母表中的_名称[4],以及\ 此字母表中的字母名称[5]: 大学错误点+=1 如果verbose为真,而辅音_错为假: 打印(“4个辅音并排检测”+此名称) 辅音_错误=正确 如果大学错误点==0: 返回此名称 def搜索_名称(txt): disDate=str(datetime.now()) 计数器=0 数字=0 尽管如此: name=name\u筛选() name_length=len(name) 标准空间=10 fin\u space=std\u space-名称\u长度 _space=”“*fin_space 如果计数器==0: 打印(str(number)+“|”,end=”“) 打印(名称+_空格,end=”“) 计数器+=1 如果计数器==10: 打印() 计数器=0 数字+=1 如果name==txt: 打印() 打印() 打印(“名称”+txt+“基于编号”+str(编号)) 打印(日期) 打印(str(datetime.now()) 打破 sys.stdout.flush() def检查名称(此名称): 大学错误点=0 错误的_点=0 对于使用此名称的y: 如果在lc_中为y: 错误的_点+=1 如果错误的\u点>=len(此\u名称): 大学错误点+=1 如果verbose为True: 打印(“检测到所有声音”+此名称) 错误的\u点\u 2=0 对于范围内的z(len(此名称)): 如果此名称[z]以lc字母表示: 错误的_点_2+=1 如果错误的\u点\u 2==len(此\u名称): 大学错误点+=1 如果verbose为True: 打印(“检测到所有辅音”+此名称) 对于范围内的v(len(此名称)-1): 如果lc_vocal中的此_名称[v]和lc_vocal中的此_名称[v+1]: 大学错误点+=1 如果verbose为True: 打印(“检测到声音并排”+此名称) 如果lc_字母表中的_名称[0]和lc_字母表中的_名称[1]以及lc_字母表中的_名称[2]: 大学错误点+=1 如果verbose为True: 打印(“3个辅音并排检测”+此名称) 如果lc_字母表中的_名称[1]和lc_字母表中的_名称[2]以及lc_字母表中的_名称[3]: 大学错误点+=1 如果verbose为True: 打印(“3个辅音并排D
def name_filtering():
    while True:
        this_name = name_generator()
        if check_name(this_name):
            return this_name
import sys
import random
import itertools
from datetime import datetime

#strings that can use random.choice
lc_consonants = "bcdfghjklmnpqrstvwxyz"
lc_vowel = "aeiou"

#sets that support fast membership testing
set_lc_consonants = set("bcdfghjklmnpqrstvwxyz")
set_lc_vowel = set("aeiou")

verbose = False

#name generator which now always produces a valid name
def name_generator():
    finished_name = []
    desired_characters = random.randrange(4, 9)
    next_is_consonant = random.random() < 0.7

    while len(finished_name) < desired_characters:
        if next_is_consonant:
            finished_name.extend(random.choice(lc_consonants) for _ in range(random.randrange(1, 3)))
        else:
            finished_name.append(random.choice(lc_vowel))

        next_is_consonant = not next_is_consonant

    return ''.join(finished_name)

#this is now redundant:
def name_filtering():
    while True:
        this_name = name_generator()
        if check_name(this_name):
            return this_name

#shortened logic: if a run of 3 is not present, we know a run of more than 3 is also not present
def check_name(this_name):
    name_len = len(this_name)
    VOWEL_WRONG = False
    CONSONANT_WRONG = False
    univ_wrong_point = 0
    wrong_point = 0
    wrong_point_2 = 0

    for i in range(name_len - 1):
        if all(this_name[i + j] in set_lc_vowel for j in range(2)):
            if verbose  and VOWEL_WRONG :
                #str.format for interpolation from now on
                print("VOWEL SIDE BY SIDE DETECTED {}".format(this_name))

            return False

    for i in range(name_len - 2):
        if all(this_name[i + j] in set_lc_consonants for j in range(3)):
            if verbose:
                print("3 consonants side by side in {}".format(consonant_run))

            return False
    return True

def search_name(txt):
    #no need for str conversion
    start_date = datetime.now()
    std_space = 10

    #counter is implemented by itertools
    for counter in itertools.count():
        name = name_generator()

        #use modular arithmetic rather than extra assignments
        if counter % 10 == 0:
            print("\n{}| ".format(counter // 10), end="")

        #use str.format for space-filling rather than arithmetic
        print("{: <{}}".format(name, std_space), end="")

        if name == txt:
            print("\n\nName {} FOUND on Number {}".format(txt, number))
            #print automatically converts a datetime to a string
            print(start_date)
            print(datetime.now())
            break
$ time python name_generator.py izaak | head -10000 | wc -l