-2
这是我写的一个程序。我只是想知道为什么我的全局变量ABSDEV_SUM
在循环结束后不打印。这个变量不打印的原因是什么?
# python_fitness_function
import pandas as pd
import itertools
infile_path = "C:\\Users\\Tim\\Dropbox\\Lela.com_DrBx\\APR14_query_work\\"
df = pd.DataFrame.from_csv(infile_path + "mp_viewed_item_AGG_affiliate_item_TOP_10.csv", sep=',', index_col=True)
groups = df.groupby('Affiliate_ID')
df_cameta = groups.get_group("cameta")
cols = list(df_cameta.columns)
while 'Affiliate_ID' in cols:
cols.remove('Affiliate_ID')
Motivator_list = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"]
index = 0
ABSDEV_SUM = 0
for i in cols:
column = df[i]
AbsDev = sum(abs(pair[1] - pair[0]) for pair in itertools.combinations(column, 2))
ABSDEV_SUM = ABSDEV_SUM + AbsDev
print "Motivator_" + Motivator_list[index] + "_Mean_AbsDev"
print AbsDev
index += 1
print ABSDEV_SUM
任何人都可以为我阐明这一点吗?对不起,如果它似乎是一个微不足道的步骤。
File "~\APR14_query_work\python_fitness_function.py", line 37, in <module>
print "Motivator_" + Motivator_list[index] + "_Mean_AbsDev"
IndexError: list index out of range
[Finished in 0.4s with exit code 1]
我认为正在发生的事情是它会通过回路的一个附加重复,抛出了“超出范围”的错误,后来干脆不:
当循环终止我得到这个代码打印,因为它终止。我不知道情况是否如此,似乎是这样,但我无法确定原因。
我试过在循环中洗牌我的行,无济于事。
您能向我们展示完整的代码吗? – fledgling
你确定'Motivator_list'至少与'cols'一样长吗? –
不,你在*循环结束之前得到了回溯*。 R2单位有一个不好的激励因素。我的意思是你的'Motivator_list'没有'cols'那么多的成员。 – kojiro