so there is a oneliner. jeez, the original code looked like a dumpster.
i came from C and i’m not very experienced in Python so i just re-wrote this C code into Python:
// len is the length of li
double mean(double li[], int len) {
double total = 0;
// you don’t need braces for oneliner for loops
for (int i = 0; i < len; i++)
total += li[i];
return total / len;
}
The numpy function is also going to be significantly faster, since it will either loop over the data in C instead of in Python, for faster looping.
If the input is in a numpy array, I believe it takes advantage of the number packing to use vector instructions in the CPU to speed it up even more. Nothing like a single SSE instruction to do a 4-wide vector addition in a single cycle.
View all comments
Show parent comment
15
u/-Redstoneboi- Aug 10 '19 edited Aug 11 '19
should it have been
?
does
totalbehave as an int instead of a float?can you make a clean oneliner to do this?
sorry, quite new to Python.
if you don't answer i guess i'll just google it.
EDIT: