2012-04-23 71 views

回答

5
d = {(i,j):f(i,j) for i in xrange(A) for j in xrange(B)} 
+0

+1,但字符'D = {....}',更精確地說:) – Walter 2012-04-23 10:42:15

+0

好的,我會改變它,但它並沒有那麼重要...... – jamylak 2012-04-23 10:43:21

2

它通常是一個好主意,用itertools.product替換嵌套循環:

from itertools import product 
d = { p: f(*p) for p in product(range(A), range(B))}