I’m just trying to write a simple script that prints between 1 and 10 words per line from a source. I know this should be super simple but I’m having a brain fart. This is my best guess:
import random
s = open("somefile.txt").readlines()
for line in s:
line.strip()
rand_int = rand.randint(1,10)
sentence = ''.join(s,rand_int)
print sentence
Specifically, I’m just having trouble figuring out how to tell python print a random group of words a random number of times per line. Is this something a list comprehension might help with?
I’d appreciate any help you could give. Thank you!
Try the following:
Here is a portion of the documentation of
random.sample():So we can create
populationby splitting the line on whitespace, and then randomizek, which should give you what you are looking for.