2

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.

zEro
  • 1,255
  • 14
  • 24
  • 3
    The usual thing to do in such cases is to use `_` as the variable name. – zondo Mar 19 '16 at 21:38
  • What is the Variable `item` used for? You don't Pass it to the function, so you don't need it... – linusg Mar 19 '16 at 21:40
  • @linusg: It is used in the list comprehension because he wants `generate_float_individual(size)` to be called `count` times. He can't do `for in range(count)`, so he does `for item in range(count)` instead. – zondo Mar 19 '16 at 21:41
  • Does generate_float_individual() return a different value each time it is called? – Antoine Pinsard Mar 19 '16 at 21:45
  • yes @AntoinePinsard, every time generate_float_individual(), returns a different value. – Gleydson Silva Mar 19 '16 at 22:49

3 Answers3

1

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
aspotic
  • 76
  • 4
0

If generate_float_individual(size) should return the same result at each call, you could just do :

return [generate_float_individual(size)] * count
Antoine Pinsard
  • 33,148
  • 8
  • 67
  • 87
  • even if the return value is equal, if it is also mutable this will not work as expected. – Tadhg McDonald-Jensen Mar 19 '16 at 21:48
  • 1
    It is worth noting that if `generate_float_individual()` returns a mutable object, all elements in the list will be modified when any one of them is. – zondo Mar 19 '16 at 21:49
  • Yes you are right, should be adapted to the use case. If it returns a list for instance, this should work `return [generate_float_individual(size)[:]] * count`, though adding the overhead of a list copy. – Antoine Pinsard Mar 19 '16 at 21:50
  • @AntoinePinsard, adding `[:]` creates a copy of the return value but now all the elements are just a reference to the one copy. It doesn't solve anything! – Tadhg McDonald-Jensen Mar 19 '16 at 21:58
  • Ah yes right, my mistake. – Antoine Pinsard Mar 19 '16 at 21:58
  • actually, i want `generate_float_population(count, size)` returns a list, wich every element of this list, is one element returned `generate_float_individual(size)`. So my list, should to have length equals to count, dont know if i was clear enough, and sorry about my english. – Gleydson Silva Mar 19 '16 at 22:06
  • The @AntoinePinsard answer solve. Thanks guys, and again i really sorry abou my english, i'm trying to be better. – Gleydson Silva Mar 19 '16 at 22:13
0

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)]
Dleep
  • 1,045
  • 5
  • 12
  • you raise an excellent point about `range` but the question very obviously states: _My IDE says, variable "item" is not used._ So this doesn't actually solve the original question. – Tadhg McDonald-Jensen Mar 24 '16 at 15:29