4

I'm working in python and currently have the following code:

list = []
for a in range(100):
    for b in range(100):
        for c in range(100):
            list.append(run(a,b,c))

where run(a,b,c) returns an integer (for example, it could multiply the three numbers together). Is there a faster way to either loop over these numbers or use a map function?

Thanks :)

1
  • an alternative, depending on what you want to do exactly with the run function, could be to use something like numpy Commented Sep 7, 2011 at 7:44

5 Answers 5

8

Have a look at the itertools-module and particulary the product method

example usage:

for i in itertools.product(range(0,100), repeat=3):
    #do stuff with i
    list.append(run(i[0],i[1],i[2]))

Note that the function call can be shortened to:

list.append(run(*i))

in the example above. see docs.python.org for explanation of Unpacking Argument Lists.

As an example, the output from product(range(0,2), repeat=3)) looks like this:

(0, 0, 0)
(0, 0, 1)
(0, 1, 0)
(0, 1, 1)
(1, 0, 0)
(1, 0, 1)
(1, 1, 0)
(1, 1, 1)
Sign up to request clarification or add additional context in comments.

2 Comments

+1 for clear code. Sure you can write it in one line but when you're explaining stuff this is what people wants to see.
I disagree. A line like list.append(run(i[0],i[1],i[2])) makes my eyes hurt a lot more than my one line solution. The short version with run(*i) is acceptable, though.
2

I think you can use imap to do this :

from itertools import imap
result = list(imap(run, range(100), range(100), range(100)))

imap yields its result... so if you want to iterate of the results don't use the list()

2 Comments

Hmm... when I try to import it like you said I get the error that I cannot import name imap. I'm using python 3.0, does this matter?
@Ben map in Python 3 behavior as imap in Python 2, and this code is equivalent to [ run(a, b, c) for a, b, c in zip(range(100), range(100), range(100)) ] instead of what you want.
1
from itertools import product
my_list = [run(a, b, c) for a, b, c in product(xrange(100), xrange(100), xrange(100))]

Or:

from itertools import product
my_list = [run(a, b, c) for a, b, c in product(xrange(100), repeat=3)]

Comments

0
import itertools    
li = [run(*triple) for triple in itertools.product(xrange(100), repeat=3)]

Comments

0

an alternative, depending on what you want to do exactly:

my_array = numpy.tile(numpy.arange(100),(3,1)).T
def run(row):
    return numpy.prod(row, 0)
[run(row) for row in my_array]

Of course, it all depends on run, e.g. if you want to take the product, you could also operate on the whole array, which is much faster:

my_product = run(my_array.T)
my_product = numpy.prod(my_array, 1)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.