Created
December 13, 2020 03:42
-
-
Save menicosia/3e28abf3ed20e522a6d497da1b1a6d44 to your computer and use it in GitHub Desktop.
Finding a random word using Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> import re | |
>>> import random | |
>>> import time | |
>>> random.seed(time.time()) | |
>>> wordHandle = open("/usr/share/dict/web2") | |
>>> words = wordHandle.readlines() | |
>>> aFilter = re.compile("^[Aa]") | |
>>> wordsWithA = list(filter(aFilter.match, words)) | |
>>> len(wordsWithA) | |
17096 | |
>>> random.choice(wordsWithA).strip() | |
'apophorometer' | |
>>> random.choice(wordsWithA).strip() | |
'auricular' | |
>>> random.choice(wordsWithA).strip() | |
'aerodrome' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment