0

So this is the code I have. The outcome is something along "['jjjjjjj', 'tt', 'dddd', 'eeeeeeeee']". How can I change the list comprehension to allow for the function random.choice to repeat for each letter of each 'word'?

lista=[random.randint(1,10)*random.choice(string.ascii_letters) for i in range(random.randint(1,10))]
1
  • Sorry I didn't get what you want. Commented Oct 16, 2016 at 15:49

2 Answers 2

2

I suppose you mean you want "abcd" and "jdhn" instead of "aaaa" and "jjjj" for the purpose of testing a sorting algorithm or some such.

If so, try this

lista=["".join(random.choice(string.ascii_letters) for j in range(random.randint(1,10)) ) for i in range(random.randint(1,10)) ]
Sign up to request clarification or add additional context in comments.

Comments

0

You'll need a nested list comorehension. You need to rerun random.choice for every letter. Something along the lines of

[ [Random.choice(string.asciiletters) for x in range(y)]  for you in range(5)]

Will give you 5 'words' each of length y.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.