你好,我有一个随机的问题,这是我的第一个帖子,所以如果我的礼节是错误的,我很抱歉:P

我正在用python做一个比赛,现在我有4辆 "汽车",每一轮他们都在1和10之间滚动,在这一轮结束后,我想显示类似 "carX以Y的滚动处于领先地位 "的东西,我不知道如何比较每辆车的滚动,并根据哪辆车的最高值打印出某个文本。

我感谢你的时间。

4 个评论
请更具体一些。你应该包括一些样本数据,以及预期的输出。
你的问题到底是什么?如何比较2个Varibales?如何打印它们?请说得更具体些。)
如果我们不知道你的代码是什么样子的,或者你的数据是如何生成/存储的,任何人都很难帮助你或给你一个答案。
只要按卷值降序排序,并取索引为0的那个。
python
robsta107
robsta107
发布于 2019-11-03
4 个回答
Vlad Keel
Vlad Keel
发布于 2019-11-03
已采纳
0 人赞同

所以,基本上,你要找的是一个 "argmax "的实现。 意思是你有N个值(卷),对应N个索引(车),你想找到哪个索引包含最高值。

不管怎么说,既然你没有提到,我就假设如果几个数值都是最大的(几辆车得到了相同的、最高的滚动),那么其中第一个被认为是赢家。

在你的案例中,鉴于N=4,你可以直接比较所有的变量。这就给了你4个案例(每辆车获胜一个),每个案例有3个比较。实际上是3个案例,因为如果前3个案例没有获胜,那么第四个案例肯定会获胜。 每个案例看起来都是这样。

if car_1 >= car_2 && car_1 > car_3 && car_1 > car_4:
    print("Car 1 is in the lead with a roll of {}".format(car_1))

这是个可怕的方法.因此,让我们首先把汽车变成一个汽车列表

list_cars = [car_1, car_2, car_3, car_4]

你可以使用numpy中已经存在的argmax函数,但让我们看看对于这个简单的案例,你如何在没有numpy的情况下做到这一点。

由于默认的'max'函数只会给你一个值(roll),而不会给你一个车的值,所以我们将给这个函数一个list_cars的索引列表,以及一个键,表示对于每个索引,使用相应的车的值。

func = lambda idx: list_cars[idx]
best_car = max(range(len(list_cars)), key = func)

这里'func'定义了一个lambda函数,对于汽车列表中的每个索引,返回汽车的值,而range(len(list_cars))给出了一个从0到list_cars长度的数字列表,所以是[0, 1, 2, 3]。

结果,'best_car'将是一个介于0和3之间的数字,是价值最高的汽车。然后你只需打印

print("Car {} is in the lead with a roll of {}".format(best_car + 1, list_cars[best_car]))

我们用best_car+1来打印汽车的编号,因为索引是从0开始计算的,而汽车是从1开始计算的。

Czylabson Asa
Czylabson Asa
发布于 2019-11-03
0 人赞同

你需要一些原始的 "数据结构 "来存储比赛的实际结果。 然后计算最高分+选择最高分的参赛者。 将其转换为python。

from random import randint
# roll will generate numbers (uniformly) between LO,HI (HI is inclusive)
LO,HI=1,10
roll=lambda : randint(LO,HI)
# generate the participiants of the race
# here the first element is the cumulative point
parti=[[0,"car"+str(i)] for i in range(n)]
print(parti)
R=10 # num of rounds
while r<R:
  for i in range(n):
    parti[i][0]+=roll()
  # compute the top point
  mx=max(v[0] for v in parti)
  # select the top contestants
  top=','.join([v[1] for v in parti if v[0]==mx])
  # report the state
  print('after the {0:d}. round {1:s} on the top with {2:d} points'.format(r,top,mx))
    
Yasuke
Yasuke
发布于 2019-11-03
0 人赞同

如果我理解得好,每一轮你都想打印出领先的汽车的名字。你可以通过创建一个Cars的字典来实现这个目的,其中的 key 是汽车的名称,而 价值 is the Cars position. Each round just update the 价值 of all the cars and find the leading car using max() function.

样品代码。

import random
number_of_rounds = 10
cars = {'Car1': 0, 'Car2':0, 'Car3':0,'Car4':0} #dictionary of cars
for round in range(1,number_of_rounds+1):
    for car in cars:
        roll_value = random.random()*9 + 1 # get the random value from 1 to 10
        roll_value = int(roll_value) #convert to int
        cars[car] = cars[car] + roll_value
    print cars
    leading_car = max(cars, key=cars.get) # find the key with highest value
    print leading_car + " is in the lead with a roll of " + str(round)
    
GardenGnome164
GardenGnome164
发布于 2019-11-03
0 人赞同

对不起,我不能发表意见,因为我的回复量很低,但我有一个可能的解决方案:如果你只想检查和输出答案,你可以使用一个while循环。

# I don't know exactly what you're using but you define your vars up here!
carX = 2
carY = 5
carZ = 3
carN = 7
# Checks every round to see if carX is in the lead
while (carX > carY and carX > carZ and carX > carN):
    print(f"Car X is in the Lead with a roll of {carX}!")
    # Re-roll and add the values again
# Checks if carY is in the lead
while (carY > carX and carY > carZ and carX > carN):
    print(f"Car Y is in the Lead with a roll of {carY}!")
    # Re-roll and add the values again
while (carZ > carX and carZ > carY and carZ > carN):
    print(f"Car Z is in the Lead with a roll of {carZ}!")
    # Re-roll and add the values again
while (carN > carX and carN > carY and carN > carZ):
    print(f"Car Z is in the Lead with a roll of {carZ}!")