随机字生成器 - Python

时间:2013-09-16 18:17:01

标签: python random python-3.3

所以我基本上正在开发一个项目,在这个项目中,计算机从一个单词列表中取出一个单词并为用户弄乱它。只有一个问题:我不想在列表中写出大量的单词,所以我想知道是否有方法导入大量的随机单词所以即使我不知道它是什么,然后我也可以享受游戏?这是整个程序的编码,它只有6个单词:

import random

WORDS = ("python", "jumble", "easy", "difficult", "answer",  "xylophone")
word = random.choice(WORDS)
correct = word
jumble = ""
while word:
    position = random.randrange(len(word))
    jumble += word[position]
    word = word[:position] + word[(position + 1):]
print(
"""
      Welcome to WORD JUMBLE!!!

      Unscramble the leters to make a word.
      (press the enter key at prompt to quit)
      """
      )
print("The jumble is:", jumble)
guess = input("Your guess: ")
while guess != correct and guess != "":
    print("Sorry, that's not it")
    guess = input("Your guess: ")
if guess == correct:
    print("That's it, you guessed it!\n")
print("Thanks for playing")

input("\n\nPress the enter key to exit")

5 个答案:

答案 0 :(得分:59)

阅读本地词汇列表

如果您反复这样做,我会在本地下载并从本地文件中提取。 * nix用户可以使用/usr/share/dict/words

示例:

word_file = "/usr/share/dict/words"
WORDS = open(word_file).read().splitlines()

从远程词典中拉出

如果您想从远程字典中提取,可以使用以下几种方法。请求库使这非常简单(您必须pip install requests):

import requests

word_site = "http://svnweb.freebsd.org/csrg/share/dict/words?view=co&content-type=text/plain"

response = requests.get(word_site)
WORDS = response.content.splitlines()

或者,您可以使用内置的urllib2。

import urllib2

word_site = "http://svnweb.freebsd.org/csrg/share/dict/words?view=co&content-type=text/plain"

response = urllib2.urlopen(word_site)
txt = response.read()
WORDS = txt.splitlines()

答案 1 :(得分:3)

Python 3的解决方案

对于Python3,以下代码从Web中获取单词列表并返回列表。答案基于凯尔凯利的accepted answer above

import urllib.request

word_url = "http://svnweb.freebsd.org/csrg/share/dict/words?view=co&content-type=text/plain"
response = urllib.request.urlopen(word_url)
long_txt = response.read().decode()
words = long_txt.splitlines()

输出:

>>> words
['a', 'AAA', 'AAAS', 'aardvark', 'Aarhus', 'Aaron', 'ABA', 'Ababa',
 'aback', 'abacus', 'abalone', 'abandon', 'abase', 'abash', 'abate',
 'abbas', 'abbe', 'abbey', 'abbot', 'Abbott', 'abbreviate', ... ]

并且生成(因为这是我的目标)一个列表1)只有大写的单词,2)只有"名字喜欢"单词,和3)一种逼真而又有趣的随机名称:

import random
upper_words = [word for word in words if word[0].isupper()]
name_words  = [word for word in upper_words if not word.isupper()]
rand_name   = ' '.join([name_words[random.randint(0, len(name_words))] for i in range(2)])

还有一些随机名称:

>>> for n in range(10):
        ' '.join([name_words[random.randint(0,len(name_words))] for i in range(2)])

    'Semiramis Sicilian'
    'Julius Genevieve'
    'Rwanda Cohn'
    'Quito Sutherland'
    'Eocene Wheller'
    'Olav Jove'
    'Weldon Pappas'
    'Vienna Leyden'
    'Io Dave'
    'Schwartz Stromberg'

答案 2 :(得分:2)

网上有很多字典文件 - 如果你在linux上,很多(全部?)发行版附带一个/ etc / dictionaries-common / words文件,你可以很容易地解析({{1 ,例如)使用。

答案 3 :(得分:2)

有一个软件包random_word可以非常方便地实现此请求:

 $ pip install random-word

from random_word import RandomWords
r = RandomWords()

# Return a single random word
r.get_random_word()
# Return list of Random words
r.get_random_words()
# Return Word of the day
r.word_of_the_day()

答案 4 :(得分:0)

在线获取字样

>>> words = requests.get("http://svnweb.freebsd.org/csrg/share/dict/words?view=co&content-type=text/plain").content.splitlines()
>>> x = 0
>>> for w in words:
...  print(str(x) + str(w).replace("'b",""))
...  x += 1

输出

25477b'zooplankton'
25478b'Zorn'
25479b'Zoroaster'
25480b'Zoroastrian'
25481b'zounds'
25482b"z's"
25483b'zucchini'
25484b'Zulu'
25485b'Zurich'
25486b'zygote'

将名称存储在本地pc

with open("dictionary.txt",'w') as file:
    for w in words:
        file.write(str(x) + str(w).replace("'b",""))