基于可变参数泛化函数
generalize a function based on variable arguments
这是我的第一个 post,我是一名初级程序员。我希望你能帮助我成为更好的人。
我正在尝试为五 (05) 名玩家建模循环多米诺骨牌游戏。在每一轮中,只有四名 (04) 球员参加比赛,两人一组 (02)。对于固定数量的 5 个玩家,下面的公式可以解决问题。
我的具体问题是如何让它对任意数量的玩家通用;以及如何使这个更有效率。 (我认为是可以的,因为我是新手)
谢谢。
Results to be modeled
代码:
import numpy
def calculo(*args):
wints= numpy.zeros(len(args),dtype=int)
for i, item in enumerate(args):
if i is 0:
if item[0]>item[1]:
wints+=[1,1,0,0,0] # players 12
else:
wints+=[0,0,1,1,0] # players 34
if i is 1:
if item[0]>item[1]:
wints+=[1,0,0,0,1] # players 15
else:
wints+=[0,1,1,0,0] # players 23
if i is 2:
if item[0]>item[1]:
wints+=[1,0,0,1,0] # players 14
else:
wints+=[0,1,0,0,1] # players 25
if i is 3:
if item[0]>item[1]:
wints+=[1,0,1,0,0] # players 13
else:
wints+=[0,0,0,1,1] # players 45
if i is 4:
if item[0]>item[1]:
wints+=[0,1,0,1,0] # players 24
else:
wints+=[0,0,1,0,1] # players 35
return wints
print(calculo([118,28],[128,66],[26,133],[111,0],[57,109]))
很难理解你要做什么,但你可以先像这样减少冗余代码:
def calculo(items):
wints = numpy.zeros(len(items), dtype=int)
if items[0][0] > items[0][1]:
wints += [1,1,0,0,0] # players 12
else:
wints += [0,0,1,1,0] # players 34
if items[1][0] > items[1][1]:
wints += [1,0,0,0,1] # players 15
else:
wints += [0,1,1,0,0] # players 23
if items[2][0] > items[2][1]:
wints += [1,0,0,1,0] # players 14
else:
wints += [0,1,0,0,1] # players 25
if items[3][0] > items[3][1]:
wints += [1,0,1,0,0] # players 13
else:
wints += [0,0,0,1,1] # players 45
if items[4][0] > items[4][1]:
wints += [0,1,0,1,0] # players 24
else:
wints += [0,0,1,0,1] # players 35
return wints
这里好像有个规律...
def one_hot_encode(players, n):
arr = numpy.zeros(n + 1, dtype=int)
arr[players] = 1
return arr[1:]
def calculo(scores, pairings, n_players):
wints = numpy.zeros(n_players, dtype=int)
for (s1, s2), (p1, p2) in zip(scores, pairings):
winner = p1 if s1 > s2 else p2
wints += one_hot_encode(winner, n_players)
return wints
scores = [[118, 28], [128, 66], [26, 133], [111, 0], [57, 109]]
pairings = [ [[1,2],[3,4]], [[1,5],[2,3]], [[1,4],[2,5]], [[1,3],[4,5]], [[2,4],[3,5]] ]
print(calculo(scores, pairings, 5))
输出:
[3 2 2 0 3]
您可以根据需要生成配对。
这是我的第一个 post,我是一名初级程序员。我希望你能帮助我成为更好的人。
我正在尝试为五 (05) 名玩家建模循环多米诺骨牌游戏。在每一轮中,只有四名 (04) 球员参加比赛,两人一组 (02)。对于固定数量的 5 个玩家,下面的公式可以解决问题。
我的具体问题是如何让它对任意数量的玩家通用;以及如何使这个更有效率。 (我认为是可以的,因为我是新手)
谢谢。
Results to be modeled
代码:
import numpy
def calculo(*args):
wints= numpy.zeros(len(args),dtype=int)
for i, item in enumerate(args):
if i is 0:
if item[0]>item[1]:
wints+=[1,1,0,0,0] # players 12
else:
wints+=[0,0,1,1,0] # players 34
if i is 1:
if item[0]>item[1]:
wints+=[1,0,0,0,1] # players 15
else:
wints+=[0,1,1,0,0] # players 23
if i is 2:
if item[0]>item[1]:
wints+=[1,0,0,1,0] # players 14
else:
wints+=[0,1,0,0,1] # players 25
if i is 3:
if item[0]>item[1]:
wints+=[1,0,1,0,0] # players 13
else:
wints+=[0,0,0,1,1] # players 45
if i is 4:
if item[0]>item[1]:
wints+=[0,1,0,1,0] # players 24
else:
wints+=[0,0,1,0,1] # players 35
return wints
print(calculo([118,28],[128,66],[26,133],[111,0],[57,109]))
很难理解你要做什么,但你可以先像这样减少冗余代码:
def calculo(items):
wints = numpy.zeros(len(items), dtype=int)
if items[0][0] > items[0][1]:
wints += [1,1,0,0,0] # players 12
else:
wints += [0,0,1,1,0] # players 34
if items[1][0] > items[1][1]:
wints += [1,0,0,0,1] # players 15
else:
wints += [0,1,1,0,0] # players 23
if items[2][0] > items[2][1]:
wints += [1,0,0,1,0] # players 14
else:
wints += [0,1,0,0,1] # players 25
if items[3][0] > items[3][1]:
wints += [1,0,1,0,0] # players 13
else:
wints += [0,0,0,1,1] # players 45
if items[4][0] > items[4][1]:
wints += [0,1,0,1,0] # players 24
else:
wints += [0,0,1,0,1] # players 35
return wints
这里好像有个规律...
def one_hot_encode(players, n):
arr = numpy.zeros(n + 1, dtype=int)
arr[players] = 1
return arr[1:]
def calculo(scores, pairings, n_players):
wints = numpy.zeros(n_players, dtype=int)
for (s1, s2), (p1, p2) in zip(scores, pairings):
winner = p1 if s1 > s2 else p2
wints += one_hot_encode(winner, n_players)
return wints
scores = [[118, 28], [128, 66], [26, 133], [111, 0], [57, 109]]
pairings = [ [[1,2],[3,4]], [[1,5],[2,3]], [[1,4],[2,5]], [[1,3],[4,5]], [[2,4],[3,5]] ]
print(calculo(scores, pairings, 5))
输出:
[3 2 2 0 3]
您可以根据需要生成配对。