加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

从python中的列表中选择数字

发布时间:2020-12-20 12:30:03 所属栏目:Python 来源:网络整理
导读:我正在制作一个能够产生单一淘汰赛的计划.到目前为止,我的代码看起来像这样(我刚刚开始) amount = int(raw_input("How many teams are playing in this tournament? "))teams = []i = 0while iamount: teams.append(raw_input("please enter team name: "))
我正在制作一个能够产生单一淘汰赛的计划.到目前为止,我的代码看起来像这样(我刚刚开始)

amount = int(raw_input("How many teams are playing in this tournament?  "))
teams = []
i = 0
while i<amount:
    teams.append(raw_input("please enter team name:  "))
    i= i+1

现在我被卡住了.我想随机选择2个数字,这些数字将选择面向对方的球队.数字根本不能重复,并且必须从1到“数量”.最有效的方法是什么?

解决方法

看看 random模块.

>>> import random
>>> teams = ['One team','Another team','A third team']
>>> team1,team2 = random.sample(teams,2)
>>> print team1
'Another team'
>>> print team2
'One team'

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读