0

I want to make for example, I have 3 keywords Apple, Orange, and Banana. I want these three keywords to be printed randomly, for example:

  1. Apple Orange Banana
  2. Orange Banana Apple
  3. Banana Apple Orange
  4. etc

until the possibilities run out

I've written to randomize the keywords, but the code I wrote can only print 1 sentence. how to print multiple sentences at once until the possibilities run out?

This is my Code

import random

words = ('Banana', 'Apple', 'Orange')
result = ''.join(random.sample(words, k=len(words)))
Adam
  • 1
  • 2
  • Does this answer your question? https://stackoverflow.com/a/5898031/3135025 – MTALY Sep 27 '22 at 23:48
  • It could also be `permutations` that might also work here? Depending on what specific behaviour you are looking for. It's not clear which one (permutation vs combination) would work better for whatever the ultimate goal is. But, `from itertools import permutations; list(permutations(words))`. Run that and you can see what that looks like. – idjaw Sep 28 '22 at 00:10

1 Answers1

0

Use itertools.permutations

import itertools
words = ('Banana', 'Apple', 'Orange')
result = list(itertools.permutations(words))
print (result)

This gets you all combinations but in same order every time you execute the code. You can shuffle it randomly using numpy

import numpy
numpy.random.shuffle (result)
flappix
  • 2,038
  • 17
  • 28