Here's my code:
def generate_float_population(count, size):
return [generate_float_individual(size) for item in range(count)]
My IDE says, variable "item" is not used. I want to know, if there is a more elegant way to do this.
Here's my code:
def generate_float_population(count, size):
return [generate_float_individual(size) for item in range(count)]
My IDE says, variable "item" is not used. I want to know, if there is a more elegant way to do this.
If the function actually has to be called each time then this would work
def generate_float_population(count, size)
return map(generate_float_individual, [size] * count)
However if the same value is produced each time then
def generate_float_population(count, size)
return [generate_float_individual(size)] * count
If generate_float_individual(size)
should return the same result at each call, you could just do :
return [generate_float_individual(size)] * count
Since the variable is local to that function... does it really matter that much that it exists? It's gonna disappear soon anyways and is very small. You should be more worried about creating a needless range(), since that creates a list of n elements (in Python 2.7 at least).
You could go around creating that using xrange, or going the long way around with something like this:
class CountDown(object):
def __init__(self, n):
self.n = n
def __iter__(self):
return self
def next(self):
if self.n <= 0:
raise StopIteration()
self.n -= 1
return self.n
And then use it with:
def generate_float_population(count, size):
return [generate_float_individual(size) for item in CountDown(count)]