I need to generate random words and thought to do this using an array, for example
import random
wordArray = ["wordOne", "wordTwo"...]
word = wordArray[random.randint(0, len(wordArray))]
Is there anywhere containing a large python array of words or do I have to do this manually?
CodePudding user response:
You can use this list: https://raw.githubusercontent.com/dwyl/english-words/master/words_alpha.txt
And you can parse it like this:
def load_words():
with open('words_alpha.txt') as word_file:
valid_words = set(word_file.read().split())
return valid_words
if __name__ == '__main__':
english_words = load_words()
# demo print
print('fate' in english_words)
CodePudding user response:
there is a library name random_word you can create a loop over and save random word in list also.
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()