如何从 python 中具有特定长度的文件中的列表中选择随机单词
How to chose a random word from a list in a file with an especific lenght in python
我是 python 的新手,实际上,我什至不是程序员,我是医生 :),作为练习的一种方式,我决定编写我的 hangman 版本。
经过一些研究,我找不到任何方法来使用模块“随机”来 return 具有特定长度的单词。作为解决方案,我编写了一个例程,在该例程中它会尝试一个随机单词,直到找到合适的长度。它适用于游戏,但我确信它是一个糟糕的解决方案,当然它会影响性能。那么,有人可以给我一个更好的解决方案吗?谢谢
有我的代码:
import random
def get_palavra():
palavras_testadas = 0
num_letras = int(input("Choose the number of letters: "))
while True:
try:
palavra = random.choice(open("wordlist.txt").read().split())
escolhida = palavra
teste = len(list(palavra))
if teste == num_letras:
return escolhida
else:
palavras_testadas += 1
if palavras_testadas == 100: # in large wordlists this number must be higher
print("Unfortunatly theres is no words with {} letters...".format(num_letras))
break
else:
continue
except ValueError:
pass
forca = get_palavra()
print(forca)
你可以
- 读取文件一次并存储内容
- 从每行中删除换行符
\n
个字符,因为它算作一个字符
- 为避免在长度不合适的行上制作
choice
,请先过滤以保留可能的行
- 如果
good_len_lines
列表没有元素你直接知道你可以停止,不需要做一百次选择
- 否则,在 good_length 个中选择一个词
def get_palavra():
with open("wordlist.txt") as fic: # 1.
lines = [line.rstrip() for line in fic.readlines()] # 2.
num_letras = int(input("Choose the number of letters: "))
good_len_lines = [line for line in lines if len(line) == num_letras] # 3.
if not good_len_lines: # 4.
print("Unfortunatly theres is no words with {} letters...".format(num_letras))
return None
return random.choice(good_len_lines) # 5.
这是一个工作示例:
def random_word(num_letras):
all_words = []
with open('wordlist.txt') as file:
lines = [ line for line in file.read().split('\n') if line ]
for line in lines:
all_words += [word for word in line.split() if word]
words = [ word for word in all_words if len(word) == num_letras ]
if words:
return random.choice(words)
我是 python 的新手,实际上,我什至不是程序员,我是医生 :),作为练习的一种方式,我决定编写我的 hangman 版本。 经过一些研究,我找不到任何方法来使用模块“随机”来 return 具有特定长度的单词。作为解决方案,我编写了一个例程,在该例程中它会尝试一个随机单词,直到找到合适的长度。它适用于游戏,但我确信它是一个糟糕的解决方案,当然它会影响性能。那么,有人可以给我一个更好的解决方案吗?谢谢
有我的代码:
import random
def get_palavra():
palavras_testadas = 0
num_letras = int(input("Choose the number of letters: "))
while True:
try:
palavra = random.choice(open("wordlist.txt").read().split())
escolhida = palavra
teste = len(list(palavra))
if teste == num_letras:
return escolhida
else:
palavras_testadas += 1
if palavras_testadas == 100: # in large wordlists this number must be higher
print("Unfortunatly theres is no words with {} letters...".format(num_letras))
break
else:
continue
except ValueError:
pass
forca = get_palavra()
print(forca)
你可以
- 读取文件一次并存储内容
- 从每行中删除换行符
\n
个字符,因为它算作一个字符 - 为避免在长度不合适的行上制作
choice
,请先过滤以保留可能的行 - 如果
good_len_lines
列表没有元素你直接知道你可以停止,不需要做一百次选择 - 否则,在 good_length 个中选择一个词
def get_palavra():
with open("wordlist.txt") as fic: # 1.
lines = [line.rstrip() for line in fic.readlines()] # 2.
num_letras = int(input("Choose the number of letters: "))
good_len_lines = [line for line in lines if len(line) == num_letras] # 3.
if not good_len_lines: # 4.
print("Unfortunatly theres is no words with {} letters...".format(num_letras))
return None
return random.choice(good_len_lines) # 5.
这是一个工作示例:
def random_word(num_letras):
all_words = []
with open('wordlist.txt') as file:
lines = [ line for line in file.read().split('\n') if line ]
for line in lines:
all_words += [word for word in line.split() if word]
words = [ word for word in all_words if len(word) == num_letras ]
if words:
return random.choice(words)