mardi 18 février 2020

taking random sample of text chunks python

I'm trying to take random samples of length k from each line in a string of text to create a list of text chunks, but I keep getting a type error even though I can print out a bunch of random numbers that appear in a list. the int() function doesn't help in this case. Am I thinking about this all wrong? does the random package have a better implementation? I know this is a really quick fix, but I guess I just don't know enough about data types to implement the right solution:

text = '''blahblahblah
blahblahblah
blahblahblah'''



import random
def randomtext(Text,k):
    chunks = []
    textspl = text.split('\n')
    tlen = len(textspl[0])-k
    for i in range(len(text)):
        samp = random.sample(tlen,1)
        chunks.append(text[i][samp:samp+k])
return chunks


randomtext(text,3)
TypeError: Population must be a sequence or set.  For dicts, use list(d) 



Aucun commentaire:

Enregistrer un commentaire