-5

For example, I have this text: This is a good reason to stop. (word counts is always greater than 5)

words = ['This', 'is', 'a', 'good', 'reason', 'to', 'stop']
flashcards_count = ciel(len(word) / 3)

The result I'd like to have:

[
'This __ a good ______ to ____.'
'____ is a ____ reason __ stop.'
'This is _ good reason to stop.'
]

As you may notice I'm try to avoid putting blanks in sequence, except if it's the last flashcard.

So for this, I shuffled the words and chunked it into 3 to make each flashcard, but the result might be like this:

[
'This is a ____ ______ __ stop.'
'____ __ _ good reason to stop.'
'This is a good reason to ____.'
]
Sam
  • 81
  • 11
  • I'm not sure I understand - do you mean that you want to shuffle a list, trying to ensure that every word has at least one 'new' word in between it and it's previous neighbours? – match Feb 10 '18 at 12:29
  • yes, exactly, it was really hard for me to explain that – Sam Feb 10 '18 at 12:39

1 Answers1

0

You can use the random module to shuffle a list of strings, then join them with a space inbewteen :

import random
list = ['This', 'is', 'a', 'good', 'reason', 'to', 'make', 'stop']
random.shuffle(list)
print " ".join(list)

Take a look at Shuffling a list of objects if you want to shuffle non-strings aswel.

Chuk Ultima
  • 987
  • 1
  • 11
  • 21