This question already has an answer here:
Let's say i have a list. I want to iterate over that list and add 2 random strings from it to an empty list. However, because this is a random choice, there is a possibility that the same string will be picked twice (As it is crucial not to pop or delete from that list the selected item). something like this:
import random
emptylist = []
somelist = ["a","b","c","d","e",]
for item in somelist:
emptylist.append(random.choice(somelist))
emptylist.append(random.choice(somelist))
How do i make sure that it won't pick, for example, "a" twice? I know it is possible in many ways but im looking for the most efficient one.
Aucun commentaire:
Enregistrer un commentaire