0

is it possible to create a method that generate a word from a to z ? cause i only know random generate word but what i want was a word that exist using python code

this is the random generate word code i have:

import random
wordLen = random.randint(1, 5)
def get_random_word(wordLen):
    word = ''

    for i in range(wordLen):
        word += random.choice('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')
    print(word)
    return word
get_random_word(wordLen)

or is it possible if i have a list of letter like example list= ['a','t','n','s','d','e','x'], then i want to generate real word from this list??

4

2 回答 2

2

你的意思是类似的吗?(未经测试)

from collections import defaultdict
from string import ascii_lowercase
from random import choice

words = defaultdict(list)
with open('/usr/share/dict/words') as fin:
    for word in fin:
        if len(word) == 5: 
            words.append(word)

for letter in ascii_lowercase:
    print letter, 'is for', choice(words[letter])

如果您不在具有/usr/share/dict/words(或者您想要更全面的列表)的系统上,那么http://www.findthatzip.com/search-10655738-hZIP/winrar-winzip-download-ukacd16.zip.htm是合理的好吧。

于 2012-10-12T11:06:28.227 回答
0

您可能需要一个单词列表供您选择...曾几何时,我需要一个单词,然后我在 Google 上搜索了类似英语词典 txt 之类的内容...找到了一些并保留了对我来说最好的

然后很明显你会弄清楚你将如何管理你的列表......不经常使用只是读取磁盘,也许存储文件开头的行数,这样你就可以选择一个数字并只读取你需要......或者将它们存储在内存中以供经常使用,可能是一个数组

于 2012-10-12T11:02:07.610 回答