亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 320 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號