Python 分析代码以使其易于理解

Python 分析代码以使其易于理解,python,Python,我得到了以下代码: def get_best_squad(position): df_copy = df.copy() store = [] for i in position: store.append([i,df_copy.loc[[df_copy[df_copy['team_position'] == i]['overall'].idxmax()]]['short_name'].to_string(index = False), df_copy[df_copy['team_posi

我得到了以下代码:

def get_best_squad(position):
df_copy = df.copy()
store = []
for i in position:
    store.append([i,df_copy.loc[[df_copy[df_copy['team_position'] == i]['overall'].idxmax()]]['short_name'].to_string(index = False), df_copy[df_copy['team_position'] == i]['overall'].max()])
    df_copy.drop(df_copy[df_copy['team_position'] == i]['overall'].idxmax(), inplace = True)
#return store
return pd.DataFrame(np.array(store).reshape(11,3), columns = ['team_position', 'short_name', 'overall']).to_string(index = False)
然后,我使用此代码测试:

squad_433 = ['GK', 'LB', 'CB', 'CB', 'RB', 'LM', 'CDM', 'RM', 'LW', 'ST', 'RW']
print ('4-3-3')
print (get_best_squad(squad_433))
结果是:每个位置的11名最佳足球运动员获得了最高的综合评分

目前我对代码的理解非常有限:

“在下面的函数中,我们首先复制已清理的数据帧。然后,我们创建一个空列表。对于队形中的每个位置(我们将使用两个字母的代码),我们将获得具有最高
总体
评级的玩家的
简称。”

我试图理解代码的每一部分试图做什么。有人能帮我处理
for
循环和
return
部分吗

如果可能的话,我还想分解代码,即使这可能会很耗时,并使我的代码更长


非常感谢你的帮助

这就是调试工具的用途。在IDE中使用调试器。如果您使用的是jupyter笔记本,请改用IDE。也许是派查姆或斯派德。