python生成随机中文_随机字生成器-Python

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'

你可能感兴趣的:(python生成随机中文)