444,100 Members | 2,979 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,100 IT Pros & Developers. It's quick & easy.

# Tuple of coordinates

 P: n/a Hi, i am using a software which uses python as its scripting language. I want to generate a list of coordinates more or less this way: for i in (beg, end, step): for j in (beg, end, step): for k in (beg, end, step): .......... Coords = ((i1,j1,k1), (i2,j2,k2), ...,(in,jn.kn)) Can anyone give me some advice on how to achieve this ? I got a little idea, but still need to keep working til i get it. Thanks in advance, Victor Jun 27 '08 #1
4 Replies

 P: n/a a = 1, 2 b = 3, 4, 5 c = 6, 7, 8, 9 coord = list() for i, j, k in zip(a, b, c): coord.append((i, j, k)) print coord Jun 27 '08 #2

 P: n/a On May 29, 10:16 pm, "victor.hera...@gmail.com" >coords = [(x,y,z) for x in range(0,10) for y in range(0,10) for z in range(0,10)] Jun 27 '08 #3

 P: n/a vi************@gmail.com wrote: Hi, i am using a software which uses python as its scripting language. I want to generate a list of coordinates more or less this way: for i in (beg, end, step): for j in (beg, end, step): for k in (beg, end, step): ......... Coords = ((i1,j1,k1), (i2,j2,k2), ...,(in,jn.kn)) Your statement of the problem makes it look like all three coordinates cover the same sequence of values. Is that true? Or does i's range (beg, end, step) differ from j's and k's. If they differ, are they all the same length? You pseudo-code makes it look like you want all combinations, but your sample output does not indicate that. Which is it, [(1,1,1), (2,2,2)] or [(1,1,1), (1,1,2),(1,2,1),(1,2,2), ...]? Depending on the answers to those questions, one of the following list comprehensions may demonstrate how to achieve a solution. >>a = range(2,6,2)b = range(3,7,2)c = range(10,14,2)print a,b,c [2, 4] [3, 5] [10, 12] >>[(i,j,k) for i,j,k in zip(a,b,c)] # Using zip process three lists in lock-step [(2, 3, 10), (4, 5, 12)] >>[(i,j,k) for i in a for j in b for k in c] #Nested loop give all possible combos. [(2, 3, 10), (2, 3, 12), (2, 5, 10), (2, 5, 12), (4, 3, 10), (4, 3, 12), (4, 5, 10), (4, 5, 12)] >>> Gary Herron > Can anyone give me some advice on how to achieve this ? I got a little idea, but still need to keep working til i get it. Thanks in advance, Victor -- http://mail.python.org/mailman/listinfo/python-list Jun 27 '08 #4

 P: n/a On 29 mayo, 17:45, Gary Herron >a = range(2,6,2) *>>b = range(3,7,2) *>>c = range(10,14,2) *>>print a,b,c [2, 4] [3, 5] [10, 12] *>>[(i,j,k) for i,j,k in zip(a,b,c)] *# *Using zip process three lists in lock-step [(2, 3, 10), (4, 5, 12)] *>>[(i,j,k) for i in a *for j in b *for k in c] * #Nested loop give all possible combos. [(2, 3, 10), (2, 3, 12), (2, 5, 10), (2, 5, 12), (4, 3, 10), (4, 3, 12), (4, 5, 10), (4, 5, 12)] *>>> Gary Herron Can anyone give me some advice on how to achieve this ? I got a little idea, but still need to keep working til i get it. Thanks in advance, Victor -- http://mail.python.org/mailman/listinfo/python-list- Ocultar texto de la cita - - Mostrar texto de la cita - Hi , i have another question. What if i wanted to make n tuples, each with a list of coordinates. For example : coords = list() for h in xrange(1,11,1): for i in xrange(1, 5, 1) : for j in xrange(1, 5, 1) : for k in xrange(1,2,1) : coords.append((i,j,k)) lista+str(h)= tuple coords print tuple(coords) so that i will have tuple1, tuple2,..., tupleN, etc. I am trying to do it the way i show you above but it is not working properly. I wish you could help me with that. Thanks again, Jun 27 '08 #5

### This discussion thread is closed

Replies have been disabled for this discussion.