model_names = ['is1', 'is5', 'is10', 'im1', 'im5', 'im10']
举个例子,
models = [0.1, 0.2, 0.1, 0.3, 0.2, 0.3]
列表的最大值为0.3,对应于型号名称“im1”和“im10”。我想要这个功能
dominant_model(models)
返回
['im1', 'im10']
,即最“主导”的模型。
这是我的代码,它完成了这项工作,但我想知道是否有更短的方法来做同样的事情而不是所有的条件:
def dominant_model(models):
m = max(models)
dom_models = [i for i,j in enumerate(models) if j==m]
for i in range(len(dom_models)):
if dom_models[i]==0:
dom_models[i]=model_names[0]
elif dom_models[i]==1:
dom_models[i]=model_names[1]
elif dom_models[i]==2:
dom_models[i]=model_names[2]
elif dom_models[i]==3:
dom_models[i]=model_names[3]
elif dom_models[i]==4:
dom_models[i]=model_names[4]
elif dom_models[i]==5:
dom_models[i]=model_names[5]
return dom_models
答案 0 :(得分:9)
找到最大def current_player_is_winner(self):
print(self.__current_player)
piece = GoPiece(self.__current_player)
print(piece)
win_count = 0
#Horizontal test
row = 0
col = 0
for col in range(self.__board_size):
for row in (range(self.__board_size)):
if self.__go_board[row][col] == piece:
win_count += 1
col += 1
print(win_count)
if win_count == 5:
return True
else:
return False
else:
row+=1
win_count = 0
后,将模型名称压缩为值,并取名称等于m = max(model)
的名称。
m
<强>演示强>
def dominant(names, values):
m = max(values)
return [name for name, value in zip(names, values) if value == m]
答案 1 :(得分:0)
使用两个循环:
def dominant_model(models):
m = max(models)
dom_models = [i for i,j in enumerate(models) if j==m]
for i in range(len(dom_models)):
for j in range(6):
if dom_models[i]==j:
dom_models[i]=model_names[j]
return dom_models