Simple random name generator in Python

I have a text file containing the first and last "syllables", shared with [part1] and [part2]:

  [part1]
 Ae
 Di
 Mo
 Fam
 [part2]
 dar
 kil
 glar
 tres

All I want to do is select a random line between [part1] and [part2], and then another random line between [part2] and the end of the file and merge them together (for example, โ€œAedarโ€, โ€œMoglarโ€) to create random names.

However, I'm not sure how to parse a text file efficiently with readline (). Is there a better way than scanning each line in a row and saving all of them in a list, where can I select a random item from?

+6
source share
4 answers

Serialize ( pickle ) the dictionary for the file.

Example:

# create the dict and save it to a file d={ 'part1':[ 'Ae', 'Di', 'Mo', 'Fam',], 'part2':[ 'dar', 'kil', 'glar', 'tres',], } import pickle f=open('syllables','w') pickle.dump(d,f) f.close() # read the dict back in from the file f1=open('syllables','r') sd=pickle.load(f1) f1.close() import random first_part=sd['part1'][random.randint(0,len(sd['part1'])-1)] second_part=sd['part2'][random.randint(0,len(sd['part2'])-1)] print '%s%s'%(first_part,second_part) 
+10
source
 import random parts = {} with open('parts.txt', 'r') as f: currentList = [] for line in f.readlines(): line = line.strip() if line.startswith('[') and line.endswith(']'): currentList = [] parts[line[1:-1]] = currentList else: currentList.append(line.strip()) for i in xrange(10): print ''.join(random.choice(parts[partName]) for partName in sorted(parts)) 

returns (randomly):

 Aekil Didar Mokil Mokil Moglar Moglar Diglar Famdar Famdar Modar 
+5
source

At some point, you will need to read the entire file if you do not know in advance how many prefixes and suffixes are. Since I assume that you will not do this, or that it may change, and you do not want to maintain a number to store it, you will have to read the file, and readline () is a good way to do this.

However, you can pre-process your text file so that it uses a different format, such as a pickle file. In other words, read the text file in the dictionary and compile this dictionary. A dictionary might look something like this:

dic = {'prefixes': ['Ae' ,'di', ...], 'suffixes': ['dar', 'kil', ...]}

Using the length of the arrays, you can determine what the maximum random number is. It should be more efficient than reading the entire row of a row for a row every time. And if not, at least it's a slightly more elegant solution.

+3
source

Changed by @eumiro script :

 #!/usr/bin/env python import fileinput import random import re from collections import defaultdict partname = '' parts = defaultdict(list) for line in fileinput.input(): line = line.rstrip() if line.startswith('[') and re.match(r'\[part\d+\]', line): partname = line else: parts[partname].append(line) parts_list = list(map(parts.get, sorted(parts))) for _ in range(10): print(''.join(map(random.choice, parts_list))) 

Exit

 Famglar Famkil Didar Ditres Aedar Famglar Ditres Famtres Ditres Modar 
+1
source

Source: https://habr.com/ru/post/886318/


All Articles