简体   繁体   English

在卡片列表中选择一个以上的随机物品

[英]Selecting more than one random item in a list of cards

I want to have two separate random selections from the same list. 我想从同一列表中选择两个单独的随机选择。 Is there any way to do this that doesn't include making separate lists? 有什么方法可以做到,但不包括单独列出列表吗?

import random


cards = [2,3,4,5,6,7,8,9,10,'King','Queen','Jack']
cards = random.choice(cards)

suits = ['Clubs', 'Hearts', 'Spades', 'Diamonds']
suits = random.choice(suits)

first_card = ("your first card is the {} of {}") .format(cards,suits)
second_card = ("your second card is the {} of {}") .format(cards,suits)

print first_card
print second_card

Output 产量

your first card is the 10 of Spades
your second card is the 10 of Spades

I want the output to be the same but the last card dealt is different from the first; 我希望输出相同,但最后一张发牌不同于第一张; two separate cards 两张独立的卡

Beforehand, generate all cards from figures and suits, by example with a list-comprehension. 事前,请从人物和西服中生成所有卡片,例如以列表理解的方式。 Then use random.sample to pick two random cards. 然后使用random.sample挑选两个随机卡。

import random

figures = [2, 3, 4, 5, 6, 7, 8, 9, 10, 'King', 'Queen', 'Jack']
suits = ['Clubs', 'Hearts', 'Spades', 'Diamonds']

cards = [(figure, suit) for figure in figures for suit in suits]

print(random.sample(cards, 2)) # [(5, 'Hearts'), (7, 'Diamonds')]

Although, I recommend you do not mix integers and strings to define your figures as this may lead to some confusion. 虽然,我建议您不要混合使用整数和字符串来定义图形,因为这可能会引起混淆。 I recommend you assign integers 11 , 12 and 13 to Jack , Queen and King respectively. 我建议你指定的整数111213 ,以JackQueenKing分别。

you can use itertools to generate all combination and then shuffle the list and take two , I have designed function for n_samples you can select n no of samples: 您可以使用itertools生成所有组合,然后将列表随机排列并取两个,我为n_samples设计了函数,您可以选择n个样本:

import random
import itertools

cards = [2,3,4,5,6,7,8,9,10,'King','Queen','Jack']


suits = ['Clubs', 'Hearts', 'Spades', 'Diamonds']

def n_random(list_1,list_2,no_of_samples):
    all_possible=[i for i in itertools.product(list_1,list_2)]
    if no_of_samples>len(all_possible):
        return 'Wrong choice'
    else:
        random.shuffle(all_possible)
        return all_possible[:2]

print(n_random(cards,suits,2))

output: 输出:

[(3, 'Diamonds'), (2, 'Spades')]

Use random.sample . 使用random.sample

random.sample(population, k) : random.sample(population, k)

Return a k length list of unique elements chosen from the population sequence or set. 返回从填充序列或集合中选择的唯一元素的k长度列表。

import random

cards = [2,3,4,5,6,7,8,9,10,'King','Queen','Jack']
chosen_cards = random.sample(cards,2)

suits = ['Clubs', 'Hearts', 'Spades', 'Diamonds']
chosen_suits = random.sample(suits,2)

first_card = ("your first card is the {} of {}") .format(chosen_cards[0],chosen_suits[0])
second_card = ("your second card is the {} of {}") .format(chosen_cards[1],chosen_suits[1])

print(first_card)
print(second_card)

This guarantees a different card every time: 这样可以保证每次都有不同的卡:

from random import randint
cards = [2,3,4,5,6,7,8,9,10,'King','Queen','Jack']
first_card = cards[randint(0, len(cards)-1)]
second_card = cards[randint(0, len(cards)-1)]
while second_card == first_card:
   second_card = cards[randint(0, len(cards)-1)]
print first_card
print second_card

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM