python - How can I get a empty list when I use the enumerate before the loop? -
salary_data list in python.when used enumerate function in loop below,everything went well.
##salary_data = enumerate(salary_data) max_label = [i i,x in enumerate(salary_data) if x==max(salary_data)] print max_label
but when use enumerate function below,i got salary_data empty list,and valueerror: max() arg empty sequence.i don't know why.
salary_data = enumerate(salary_data) max_label = [i i,x in salary_data if x==max(salary_data)] print max_label
enumerate(salary_data)
iterator because wrapped in enumerate
. means exhausted (empty) when call max
on (besides giving wrong maximum).
you should calculate max front (avoiding recalculation) , iterate on enumerate
d data:
max_ = max(salary_data) # avoids repeated maximum calculation in loop salary_data_enumerated = enumerate(salary_data) # other variable name, readability max_label = [i i, x in salary_data_enumerated if x==max_]
Comments
Post a Comment