简体   繁体   English

列表中的两个不同的random.choices(python)

[英]Two different random.choices from a list (python)

Complete python noob here but I am slowly wrapping my head around it. 这里完成了python noob,但我慢慢地绕着它缠着头。 I am making a 1v1 Halo 3 tournament style program that randomly matches players and picks the map, gametype etc... 我正在制作1v1 Halo 3锦标赛风格程序,随机匹配玩家并选择地图,游戏类型等......

The problem I am having is that I don't know how to randomly recall two different strings from a list. 我遇到的问题是我不知道如何从列表中随机调用两个不同的字符串。

playerList = ["Player 1", "Player 2", "Player 3", "Player 4"]

So say this was the list of players, how would I print: 所以说这是玩家名单,我将如何打印:

Player 1 vs Player 3 玩家1对玩家3

I have tried a few different things to no avail. 我尝试了一些不同的东西无济于事。 I do have a little bit of an understanding of the random library but I can't figure out exactly how to do this 我对random库有一点了解,但我无法弄清楚如何做到这一点

ie print random.choice(playerList) + " vs " + random.choice(playerList) eventually will have the same players versing each other when you've run it enough times... 即打印random.choice(playerList)+“vs”+ random.choice(playerList)当你运行足够多次时,最终将拥有彼此相同的玩家...

Cheers 干杯

What you are looking for is random.sample . 您正在寻找的是random.sample You want to pick 2 out of the list: 你想从列表中选择2个:

import random

playerList = ["Player 1", "Player 2", "Player 3", "Player 4"]
player1, player2 = random.sample(playerList, 2)
print '{} vs. {}'.format(player1, player2)

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

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