list comprehension return a list and sum over in loop

Christian Gollwitzer auriocus at gmx.de
Fri Dec 12 04:09:17 EST 2014


Am 12.12.14 09:30, schrieb KK Sasa:
> Mark Lawrence於 2014年12月12日星期五UTC+8下午3時17分43秒寫道:
> Hi Mark and Yotam, Thanks for kind reply. I think I didn't make my
> problem clear enough. The slow part is "[d2(t[k]) for k in
> xrange(1000)]". In addition, I don't need to construct a list of 1000
> lists inside, but my aim is to get the sum of all "d2(t[k])". I
> wonder if there is any method to sum up efficiently.

Not sure I understand what you need, but it seems that NumPy would be a 
more efficient method. numpy.sum can sum elements along every dimensions 
of a higher-dimensional matrix, and in general NumPy stores the elements 
in native doubles instead of Python lists, which improves both 
performance and memory usage
	
	Christian




More information about the Python-list mailing list