我想对一个由多个学科组成的攀岩比赛进行建模,以便根据两个学科之后的表现来计算一个人赢得总冠军的可能性和概率。我使用的熊猫数据框如下所示:
ranking_after_boulder
Out[34]:
Name Speed Boulder Lead Score
Rank
1.0 Ito 4 1 1 4
2.0 Kaplina 1 8 1 8
3.0 Condie 2 6 1 12
4.0 Charnoudie 3 5 1 15
5.0 Mori 8 2 1 16
6.0 Rakovec 6 3 1 18
7.0 Rogora 5 4 1 20
8.0 Krampel 7 7 1 49
然后,我根据最后一个学科中结果的可能排列来计算最终结果的所有可能场景的列表。到目前为止,有效的方法是为一个人计算可能的结果。这是我使用的代码:
def still_possible(scenarios, athlete, position):
"""
Input: -A list of all possible scenarios
name of the competitor (str) and the rank which is to be tested (int).
Output: A tuple consisting of three elements:
- A list of all possible scenarios in which athlete places equal or
better than the input rank
-Percentage of scenarios for which this is the case
-Mean position in lead that was required for athlete to achieve input rank
"""
scenarios_ok = []
necessary_positions = []
for i in scenarios:
if i.loc[i.Name == athlete].index <= position:
scenarios_ok.append(i)
necessary_positions.append(int(i.loc[i.Name == athlete, 'Lead']))
print(i)
percentage = 100 * (len(scenarios_ok) / len(scenarios))
if len(necessary_positions) > 0:
necessary_positions_mean = round(sum(necessary_positions)/len(necessary_positions), 4)
necessary_positions_max = max(necessary_positions)
else:
necessary_positions_mean = "not possible"
necessary_positions_max = "not possible"
return (scenarios_ok, percentage, necessary_positions_mean, necessary_positions_max)
到目前为止可以找到该作品。
问题: 现在,我想增加一种可能性,即按给定设置一个或多个(或不设置)结果(作为参数传递给函数,我正在考虑使用字典),但是在if和for循环中我总是迷路。我要执行的逻辑如下:
scenarios_ok = [i for i in scenarios if i.loc[i.Name == athlete].index <= position and if i.loc[i.Name == name].index = rank for name, rank in given.items()]
但这当然是无效的语法。
我希望有人对如何使其工作有所了解!谢谢!