0

Let's imagine I have a set of 54 cards.

I want to make an initial distribution of 2 cards.

Then, I want to make a new distribution of 3 cards BUT the three new cards have to be different from the 2 cards distributed initially.

And so on...

Globally, at each distribution, the distributed cards come out of the 52-card deck. There is like a update of the 52-card deck at each distribution.

Here is my code but I don't know Python well enough to find a solution despite much research.

import random

cards = ["2 coeur", "3 coeur", "4 coeur", "5 coeur", "6 coeur", "7 coeur", "8 coeur", "9 coeur", "10 coeur", "Valet coeur", "Dame coeur", "Roi coeur", "A1 coeur",
          "2 trèfle", "3 trèfle", "4 trèfle", "5 trèfle", "6 trèfle", "7 trèfle", "8 trèfle", "9 trèfle", "10 trèfle", "Valet trèfle", "Dame trèfle", "Roi trèfle", "A1 trèfle",
          "2 carreau", "3 carreau", "4 carreau", "5 carreau", "6 carreau", "7 carreau", "8 carreau", "9 carreau", "10 carreau", "Valet carreau", "Dame carreau", "Roi carreau", "A1 carreau",
          "2 pique", "3 pique", "4 pique", "5 pique", "6 pique", "7 pique", "8 pique", "A9 pique", "10 pique", "Valet pique", "Dame pique", "Roi pique", "A1 pique"]

first_card = (random.choices(cartes, k = 1))
second_card = (random.choices(cartes, k = 1))

first_distri = first_card + second_card

print(first_distri)

second_distri = (random.choices(cartes, k = 3)) #AND ????

I thought about a command that would like an "exept" for instance :

second_distri = (random.choices(cartes, k = 3)) EXCEPT first_distri.

But I'm not sure it exists.

I tried different things with if/else but nothing worked.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Sooon
  • 3
  • 1
  • You could just `remove` the cards so you are always left with the ones you can use. Also instead of doing `choices(l, k=1)` you can just use `choice(l)` – Tomerikoo Dec 27 '21 at 10:57
  • You could also just `shuffle` the list and iterate over it sequentially – Tomerikoo Dec 27 '21 at 10:58

2 Answers2

0

after initializing first_card, remove it from the list then the second and so on and so forth

Feras Alfrih
  • 492
  • 3
  • 11
0

You could remove the cards already chosen from your list with:

cards.remove(first_card)
cards.remove(second_card)

Alternatively, in line with your "except" (set subtraction-y) line of thought:

second_distri = (random.sample([card for card in cartes if card not in first_distri], k = 3))

Using sample here instead of choices by @Tomerikoo's recommendation to avoid choosing the same card twice.

Also noting by @Tomerikoo's recommendation: this method requires several iterations over the list (once to choose a card with choices, and then once more to remove it), so a more efficient solution would be to shuffle the list (with the shuffle function of the random module), pick the first (or last) element(s) of it, and then slice the part of it that contains those elements.

Shay
  • 587
  • 4
  • 13
  • It's what I tried but here is the error: ValueError: list.remove(x): x not in list – Sooon Dec 27 '21 at 11:05
  • @Sooon: change your `cartes.choices` into `cartes.choice` or, alternatively: `cards.remove(first_card[0])`. `choices` returns a list. :) – Shay Dec 27 '21 at 11:11
  • Note that [`choices`](https://docs.python.org/3/library/random.html#random.choices) is with replacement. So `second_distri` might have duplications within itself. You probably want to use [`sample`](https://docs.python.org/3/library/random.html#random.sample) – Tomerikoo Dec 27 '21 at 11:53
  • Note also that using `remove` is not so efficient. You need to first iterate the list with `choices`, then iterate again to find and remove the item. A better approach would be to simply [`shuffle`](https://docs.python.org/3/library/random.html#random.shuffle) the list and then iterate/slice it according to the needs (as described in the duplicate) – Tomerikoo Dec 27 '21 at 11:54
  • Thanks for the notes, @Tomerikoo! I added them to the answer, with proper credits. :) I had considered the `remove` matter but figured I'd opt for simplicity (in the sense that it doesn't deviate much from OP's way of thinking; the idea was to help them see how to construct that "bridge" between idea and implementation), but you're right: it should be noted. As for `choices`, I hardly ever got to use this function so I wasn't aware. Thanks for the heads up! – Shay Dec 27 '21 at 12:01