I am wondering if the 3 for loops in the following code can be written in a better way:
Nc = 10 # number of points for (0, pi)
cc1 = linspace(0,pi,Nc)
cc2 = linspace(0,pi/2,Nc/2)
cc3 = linspace(0,pi/2,Nc/2)
for c1 in cc1:
for c2 in cc2:
for c3 in cc3:
print c1,c2,c3
2 Answers 2
import itertools
for a,b,c in itertools.product(cc1, cc2, cc3):
print a,b,c
answered Aug 20, 2012 at 4:13
Amber
532k89 gold badges643 silver badges558 bronze badges
try this :)
[(c1, c2, c3) for c1 in cc1 for c2 in cc2 for c3 in cc3]
answered Aug 20, 2012 at 4:13
blueiur
1,5071 gold badge11 silver badges17 bronze badges
Comments
lang-py
numpy.meshgrid()or similar is not appropriate for the other question.