根据条件追加到列表,但避免添加重复项

2024-10-17 00:31:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图根据每个球员的统计数据,为MLB球队建立一个军刀优化的阵容顺序。我有一个统计数据框架,我从中提取数据并按重要性顺序添加到一个空列表中,然后重新排序列表以创建击球阵容

以下是击球顺序:

  1. 最高OBP
  2. 最高老年退休金
  3. 第二高SLG
  4. 第二高的老年退休金
  5. 最高SLG
  6. 第三高行动
  7. 第四高的老年退休金
  8. 第五高
  9. 第六高

现在-球员在阵容中获得位置的顺序对优化也很重要。顺序是2,4,1,5,3,6,7,8,9。因此,阵容中的第二个位置是最重要的,因此OPS最高的个人应该首先被添加到列表中,这样他就不会根据他的统计数据被添加到阵容中的任何其他位置

所以,我有一个空列表,我开始根据最重要的玩家的统计数据来提取他们,然后从数据框中删除他们,这样我就可以再次提取,而无需选择他们两次

这是我的密码:

opt_lineup = []
opt_lineup.append((chosen_team[chosen_team['OPS']==chosen_team['OPS'].max()]['Player']))
chosen_team.drop(chosen_team['OPS'].idxmax(), inplace = True)
opt_lineup.append((chosen_team[chosen_team['OPS']==chosen_team['OPS'].max()]['Player']))
chosen_team.drop(chosen_team['OPS'].idxmax(), inplace = True)
opt_lineup.append((chosen_team[chosen_team['OBP']==chosen_team['OBP'].max()]['Player']))
chosen_team.drop(chosen_team['OBP'].idxmax(), inplace = True)
opt_lineup.append((chosen_team[chosen_team['SLG']==chosen_team['SLG'].max()]['Player']))
chosen_team.drop(chosen_team['SLG'].idxmax(), inplace = True)
opt_lineup.append((chosen_team[chosen_team['SLG']==chosen_team['SLG'].max()]['Player']))
chosen_team.drop(chosen_team['SLG'].idxmax(), inplace = True)
chosen_team.head(30)

我的问题是,当我将一名球员添加到空名单时,如果两名球员平局获得最高统计数据,那么他们都会被添加到名单中,而我只想获得第一名

本质上-我正在寻找一种类似于keep = 'first'的解决方案,在使用.drop()时使用,但在添加到列表时使用

谢谢


Tags: 列表顺序maxteamopsdrop统计数据player
2条回答

要确保只选择max条目中的一个条目(第一个条目),而不是选择与max值匹配的所有条目,可以修改代码以使用^{},类似于对.drop执行的操作

你可以从^{}的正式文件中看到:

If multiple values equal the maximum, the first row label with that value is returned.

opt_lineup = []
opt_lineup.append(chosen_team.loc[chosen_team['OPS'].idxmax(), 'Player'])   #changed
chosen_team.drop(chosen_team['OPS'].idxmax(), inplace = True)
opt_lineup.append(chosen_team.loc[chosen_team['OPS'].idxmax(), 'Player'])   #changed
chosen_team.drop(chosen_team['OPS'].idxmax(), inplace = True)
opt_lineup.append(chosen_team.loc[chosen_team['OBP'].idxmax(), 'Player'])   #changed
chosen_team.drop(chosen_team['OBP'].idxmax(), inplace = True)
opt_lineup.append(chosen_team.loc[chosen_team['SLG'].idxmax(), 'Player'])   #changed
chosen_team.drop(chosen_team['SLG'].idxmax(), inplace = True)
opt_lineup.append(chosen_team.loc[chosen_team['SLG'].idxmax(), 'Player'])   #changed
chosen_team.drop(chosen_team['SLG'].idxmax(), inplace = True)
chosen_team.head(30)

请注意,我还将您的代码更改为使用^{}格式,例如

chosen_team.loc[chosen_team['OPS'].idxmax(), 'Player']

而不是使用以下格式:

chosen_team[chosen_team['OPS'].idxmax()]['Player']

这是为了更好地避免COPYWARNING设置,还可以获得更好的执行时间/内存利用率

所以你的问题是多个玩家拥有最高的属性,而你只想添加第一个。max()方法返回列表中的最高值,list.index()方法查找列表中给定值的第一个索引。因此,让我们找到max ops值(例如),并在我们将使用的索引的列表中找到该值的第一个索引

下面是一个代码示例:

ops_list = [400, 500, 500, 243]

max_ops = max(ops_list)#finds max value in the above list

max_index = ops_list.index(max_obs)#gets first index with the max value we found

battingorder[1] = players[max_index]#Assuming battingorder is defined and players has synced indices with the stat lists

编辑:在将索引添加到击球顺序后,确保从每个列表中删除索引,否则您可能会得到重复的球员

相关问题 更多 >