【问题标题】:Can I limit how often an element is randomly chosen from a list?我可以限制从列表中随机选择元素的频率吗?
【发布时间】:2022-06-15 04:32:39
【问题描述】:

我正在尝试编写一个比赛装置应用程序,该应用程序需要一定数量的球队,让他们相互对抗,而不是连续重复同一支球队两次,即让同一支球队在两天内比赛两次 例如,如果列出的球队是“a”、“b”、“c”、“d”和“e”,并且他们要进行四场比赛,则此代码会打印出类似的结果

a 与 b

c 与 d

c 与 e

但我试图阻止它连续两次重复相同的元素,即

c 与 d

c 与 e

而是

a 与 b

c 与 d

e vs a

代码如下,我需要做哪些修改

import random

number_of_teams = int(input('How many teams? : '))
other_number = number_of_teams + 1
teams = []

number_of_matches = int(input("How many matches are to be played?: "))
real_number_of_matches = number_of_matches + 1

for i in range(1, other_number):
    team_name = input("Input team name: ")
    teams.append (team_name)
print('The teams participating are', teams)


for i in range(1, real_number_of_matches):
    first_team = random.choice(teams)
    second_team = random.choice(teams)
    if first_team != second_team:
        print(f'{first_team} vs {second_team}')```

【问题讨论】:

  • 将之前选择的团队保存在一个变量中,并确保它与当前选择的团队之一不同
  • 我很确定必须有现有的表来设置比赛中理想的球队对,从而优化两场比赛之间的时间差。它可能不是随机的,而是精心设计的。

标签: python list loops random iteration


【解决方案1】:

我相信我找到了方法。

我创建了一个名为prev_teams 的字典,其中存储了以前使用的团队(我们不想在下一回合使用它们)。默认情况下,它们设置为无,因为我们没有以前的团队。

然后我创建一个团队副本并将其存储到unique_teams。我们使用独特的团队来创建我们可以从中选择的团队列表。 (使用prev_teams 从该列表中删除以前的团队,即

unique_teams = [t for t in unique_teams if t not in prev_teams.values()]

然后,使用 while 循环,我们可以检查团队是否相同,如果不是则中断

while True:
    first_team = random.choice(unique_teams)
    second_team = random.choice(unique_teams)
    if first_team != second_team:
        break

然后我们需要将之前的团队存储在prev_teams 以供以后使用

prev_teams["first"] = first_team
prev_teams["second"] = second_team

所以最终的代码是

import random

number_of_teams = int(input('How many teams? : '))
other_number = number_of_teams + 1
teams = []

number_of_matches = int(input("How many matches are to be played?: "))
real_number_of_matches = number_of_matches + 1

for i in range(1, other_number):
    team_name = input("Input team name: ")
    teams.append (team_name)
print('The teams participating are', teams)


prev_teams = {"first": None, "second": None}
print(real_number_of_matches)
for i in range(1, real_number_of_matches):
    unique_teams = teams.copy()
    #if prev_teams values occur in unique_teams, remove them
    unique_teams = [t for t in unique_teams if t not in prev_teams.values()]
    while True:
        first_team = random.choice(unique_teams)
        second_team = random.choice(unique_teams)
        if first_team != second_team:
            break
    prev_teams["first"] = first_team
    prev_teams["second"] = second_team
    print(f'{first_team} vs {second_team}')

输出

How many teams? : 8
How many matches are to be played?: 10
Input team name: a
Input team name: b
Input team name: c
Input team name: d
Input team name: e
Input team name: f
Input team name: g
Input team name: h
The teams participating are ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
11
e vs c
h vs d
g vs b
c vs f
e vs d
h vs g
c vs e
f vs d
e vs a
h vs f

【讨论】:

    猜你喜欢
    • 2018-03-25
    • 2013-10-19
    • 1970-01-01
    • 2021-12-30
    • 2014-02-07
    • 2012-03-12
    • 2023-01-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多