为了账号安全,请及时绑定邮箱和手机立即绑定

Python脚本生成具有特定结构和字母组合的单词

Python脚本生成具有特定结构和字母组合的单词

慕田峪4524236 2023-09-05 19:48:43
我想编写一个非常短的脚本,它将帮助我生成具有以下品质的随机/无意义单词:-有 8 个字母-第一个字母是“A”-第二个和第四个字母是随机字母 -第五个字母是元音-第六个和第七个字母是随机字母并且相同 -第八个字母是不是“a”的元音这是我到目前为止所尝试过的(使用我可以在网上找到和理解的所有信息)firsts = 'A'seconds = ['a','b','c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']thirds = ['a', 'e', 'i', 'o', 'u', 'y']fourths = ['a','b','c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']fifths = ['a', 'e', 'i', 'o', 'u', 'y']sixths = sevenths =  ['a','b','c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']eighths = ['e', 'i', 'o', 'u', 'y']print [''.join(first, second, third, fourth, fifth)       for first in firsts       for second in seconds       for third in thirds       for fourth in fourths       for fifth in fifths       for sixth in sixths       for seventh in sevenths       for eighth in eighths]然而它一直显示SyntaxError: invalid syntax在之后for,现在我完全不知道如何使这项工作有效。如果可以的话请帮我研究一下,非常感谢!
查看完整描述

4 回答

?
慕侠2389804

TA贡献1719条经验 获得超6个赞

这将实现您所描述的内容。您可以通过将选项放入一个整体列表中而不是使用多个不同的变量来使代码更加整洁,但是您必须明确处理第六个和第七个字母相同的事实;不能仅仅因为每个人都有相同的选择就保证它们是相同的。


该列表choices_list可以包含每个原始代码的子列表,但是当您选择单个字符时,它在使用时将与字符串同等工作random.choice,这也使代码更加整洁。


import random


choices_list = [

    'A',

    'abcdefghijklmnopqrstuvwxyz',

    'aeiouy',

    'abcdefghijklmnopqrstuvwxyz',

    'aeiouy',

    'abcdefghijklmnopqrstuvwxyz',

    'eiouy'

    ]


letters = [random.choice(choices) for choices in choices_list]


word = ''.join(letters[:6] + letters[5:])  # here the 6th letter gets repeated


print(word)

一些示例输出:


Alaeovve

Aievellu

Ategiwwo

Aeuzykko


查看完整回答
反对 回复 2023-09-05
?
互换的青春

TA贡献1797条经验 获得超6个赞

这是语法修复:


print(["".join([first, second, third]) 

 for first in firsts 

 for second in seconds 

 for third in thirds])

此方法可能会占用大量内存。


查看完整回答
反对 回复 2023-09-05
?
白衣非少年

TA贡献1155条经验 获得超0个赞

有几件事需要考虑。首先,该str.join()方法接受一个可迭代对象(例如 a list),而不是一堆单独的元素。正在做


''.join([first, second, third, fourth, fifth])

修复了这方面的程序。如果您使用的是 Python 3,print()则 是一个函数,因此您应该在整个列表推导式周围添加括号。


摆脱语法障碍,让我们来解决一个更有趣的问题:您的程序构造每个(82255680!)可能的单词。这需要很长的时间和记忆。您想要的可能只是选择一个。当然,您可以通过首先构建所有,然后随机选择一个来做到这一点。firsts不过,从、seconds等中随机挑选一个字母然后收集这些字母要便宜得多。那么大家一起:


import random

  

firsts = ['A']

seconds = ['a','b','c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

thirds = ['a', 'e', 'i', 'o', 'u', 'y']

fourths = ['a','b','c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

fifths = ['a', 'e', 'i', 'o', 'u', 'y']

sixths = sevenths =  ['a','b','c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

eighths = ['e', 'i', 'o', 'u', 'y']


result = ''.join([

    random.choice(firsts),

    random.choice(seconds),

    random.choice(thirds),

    random.choice(fourths),

    random.choice(fifths),

    random.choice(sixths),

    random.choice(sevenths),

    random.choice(eighths),

])

print(result)

要从此处改进代码,请尝试:


找到一种比显式写出“数据”更简洁的方法来生成“数据”。举个例子:

import string

seconds = list(string.ascii_lowercase)  # you don't even need list()!

不要使用单独的变量firsts、seconds等,而是将它们收集到单个变量中,例如将list每个原件list作为单个变量包含的单个变量str,并包含所有字符。


查看完整回答
反对 回复 2023-09-05
?
慕桂英4014372

TA贡献1871条经验 获得超13个赞

尝试这个:


import random

sixth=random.choice(sixths)

s='A'+random.choice(seconds)+random.choice(thirds)+random.choice(fourths)+random.choice(fifths)+sixth+sixth+random.choice(eighths)

print(s)

输出:


Awixonno

Ahiwojjy

etc


查看完整回答
反对 回复 2023-09-05
  • 4 回答
  • 0 关注
  • 123 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信