Tuples part 2

V

victor.herasme

Hi Everyone,

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,
 
B

bearophileHUGS

(e-mail address removed):

Do you mean something like this? (notice the many formatting
differences, use a formatting similar to this one in your code)

coords = []

for i in xrange(1, 5):
for j in xrange(1, 5):
for k in xrange(1, 2):
coords.append( (i, j, k) )

coords *= 10
print coords

Bye,
bearophile
 
V

victor.herasme

(e-mail address removed):

Do you mean something like this? (notice the many formatting
differences, use a formatting similar to this one in your code)

coords = []

for i in xrange(1, 5):
for j in xrange(1, 5):
for k in xrange(1, 2):
coords.append( (i, j, k) )

coords *= 10
print coords

Bye,
bearophile

Hi,

the result i would like is similar to a set of n tuples: tuple_1,
tuple_2,...,tuple_n. I use h in order to enumerate the tuples and
i,j,k would be the coordinates. Maybe something like:

tuple_1=((a,b,c),..,(n,n,n))
..
..
..
tuple_n=((d,e,f),..,(n,n,n))

I hope u can help me with that.

Victor
 
G

George Sakkis

(e-mail address removed):
Do you mean something like this? (notice the many formatting
differences, use a formatting similar to this one in your code)
coords = []
for i in xrange(1, 5):
for j in xrange(1, 5):
for k in xrange(1, 2):
coords.append( (i, j, k) )
coords *= 10
print coords
Bye,
bearophile

Hi,

the result i would like is similar to a set of n tuples: tuple_1,
tuple_2,...,tuple_n. I use h in order to enumerate the tuples and
i,j,k would be the coordinates.

From the pseudocode you wrote at first, tuple_1, tuple_2, ..., tuple_n
would be all equal. Is this intentional, and if so, what's the
purpose ?

George
 
I

Ivan Illarionov

Hi Everyone,

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,
((1, 1, 1), (1, 2, 1), (1, 3, 1), (1, 4, 1), (2, 1, 1), (2, 2, 1), (2,
3, 1), (2
, 4, 1), (3, 1, 1), (3, 2, 1), (3, 3, 1), (3, 4, 1), (4, 1, 1), (4, 2,
1), (4, 3
, 1), (4, 4, 1))

Does this help?

But I don't understand why you need this?

Ivan
 
V

victor.herasme

((1, 1, 1), (1, 2, 1), (1, 3, 1), (1, 4, 1), (2, 1, 1), (2, 2, 1), (2,
3, 1), (2
, 4, 1), (3, 1, 1), (3, 2, 1), (3, 3, 1), (3, 4, 1), (4, 1, 1), (4, 2,
1), (4, 3
, 1), (4, 4, 1))

Does this help?

But I don't understand why you need this?

Ivan

Hi,

What i need is, for example:

tuple 1=((1, 1, 1), (1, 2, 1), (1, 3, 1), (1, 4, 1))

tuple 2=((2, 1, 1), (2, 2, 1), (2, 3, 1), (2, 4, 1))

tuple 3=((3, 1, 1), (3, 2, 1), (3, 3, 1), (3, 4, 1))

and so on. Please help me and sorry for not taking the time to post my
questions properly.

Victor
 
I

Ivan Illarionov

On 5 ÉÀÎ, 01:57, "(e-mail address removed)" <[email protected]>
wrote:
((1, 1, 1), (1, 2, 1), (1, 3, 1), (1, 4, 1), (2, 1, 1), (2, 2, 1), (2,
3, 1), (2
, 4, 1), (3, 1, 1), (3, 2, 1), (3, 3, 1), (3, 4, 1), (4, 1, 1), (4, 2,
1), (4, 3
, 1), (4, 4, 1))
Does this help?
But I don't understand why you need this?

Hi,

What i need is, for example:

tuple 1=((1, 1, 1), (1, 2, 1), (1, 3, 1), (1, 4, 1))

tuple 2=((2, 1, 1), (2, 2, 1), (2, 3, 1), (2, 4, 1))

tuple 3=((3, 1, 1), (3, 2, 1), (3, 3, 1), (3, 4, 1))

and so on. Please help me and sorry for not taking the time to post my
questions properly.

Victor


coords = [(i, tuple((i,j,k) for j in range(1,5) for k in range(1,2))) for i in range(1,5)]
locals().update(("tuple_%s" % i, coord) for i, coord in coords)
tuple_1 ((1, 1, 1), (1, 2, 1), (1, 3, 1), (1, 4, 1))
tuple_2 ((2, 1, 1), (2, 2, 1), (2, 3, 1), (2, 4, 1))
tuple_3
((3, 1, 1), (3, 2, 1), (3, 3, 1), (3, 4, 1))


Is this what you want?

Ivan
 
I

Ivan Illarionov

Hi,

What i need is, for example:

tuple 1=((1, 1, 1), (1, 2, 1), (1, 3, 1), (1, 4, 1))

tuple 2=((2, 1, 1), (2, 2, 1), (2, 3, 1), (2, 4, 1))

tuple 3=((3, 1, 1), (3, 2, 1), (3, 3, 1), (3, 4, 1))

and so on. Please help me and sorry for not taking the time to post my
questions properly.

Victor

Or even so:

locals().update(("tuple_%s" % i, tuple((i,j,k) for j in range(1,5) for
k in range(1,2))) for i in range(1,5))


Ivan
 
I

Ivan Illarionov

Or even so:

locals().update(("tuple_%s" % i, tuple((i,j,k) for j in range(1,5) for
k in range(1,2))) for i in range(1,5))

Ivan

Tried to make it readable:

def iter_coords(i):
for j in xrange(1,5):
for k in xrange(1,2):
yield i, j, k

def iter_vars():
for i in xrange(1, 5):
yield "tuple_%s" % i, tuple(iter_coords(i))

locals().update(dict(iter_vars()))
((4, 1, 1), (4, 2, 1), (4, 3, 1), (4, 4, 1))

Ivan
 
G

George Sakkis

Tried to make it readable:

def iter_coords(i):
for j in xrange(1,5):
for k in xrange(1,2):
yield i, j, k

def iter_vars():
for i in xrange(1, 5):
yield "tuple_%s" % i, tuple(iter_coords(i))

locals().update(dict(iter_vars()))

locals().update() works by accident here because it's in global scope;
it doesn't work within a function.

Use a proper data structure, like a dict or a list, and access each
tuple list as 'tuples[n]' instead of 'tuple_n'.

George
 
I

Ivan Illarionov

Tried to make it readable:
def iter_coords(i):
for j in xrange(1,5):
for k in xrange(1,2):
yield i, j, k
def iter_vars():
for i in xrange(1, 5):
yield "tuple_%s" % i, tuple(iter_coords(i))
locals().update(dict(iter_vars()))

locals().update() works by accident here because it's in global scope;
it doesn't work within a function.

Use a proper data structure, like a dict or a list, and access each
tuple list as 'tuples[n]' instead of 'tuple_n'.

George

OP wanted variables and I showed him how to do this. I agree that a
list or a dict would be better.

Ivan
 
G

George Sakkis

locals().update() works by accident here because it's in global scope;
it doesn't work within a function.
Use a proper data structure, like a dict or a list, and access each
tuple list as 'tuples[n]' instead of 'tuple_n'.

OP wanted variables and I showed him how to do this. I agree that a
list or a dict would be better.

Ivan

Generating variable names at runtime doesn't work for locals and it is
a bad solution for globals in 99.9% of the cases. It is usually more
helpful to point someone who can't even express his problem clearly to
the right direction, rather than taking his pseudocode literally and
coming up with a semi-working translation.

George
 
I

Ivan Illarionov

On 5 июн, 18:19, "(e-mail address removed)" <[email protected]>
wrote:
On 5 ÉÀÎ, 01:57, "(e-mail address removed)" <[email protected]>
wrote:
Hi Everyone,
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,
from itertools import repeat, izip
coords = tuple((i,j,k) for i in xrange(1,5) for j in xrange(1,5) for k in xrange(1,2))
locals().update(("tuple%s" % i, coord) for i, coord in izip(xrange(1,11), repeat(coords)))
tuple1
((1, 1, 1), (1, 2, 1), (1, 3, 1), (1, 4, 1), (2, 1, 1), (2, 2, 1), (2,
3, 1), (2
, 4, 1), (3, 1, 1), (3, 2, 1), (3, 3, 1), (3, 4, 1), (4, 1, 1), (4, 2,
1), (4, 3
, 1), (4, 4, 1))
Does this help?
But I don't understand why you need this?
Ivan
Hi,
What i need is, for example:
tuple 1=((1, 1, 1), (1, 2, 1), (1, 3, 1), (1, 4, 1))
tuple 2=((2, 1, 1), (2, 2, 1), (2, 3, 1), (2, 4, 1))
tuple 3=((3, 1, 1), (3, 2, 1), (3, 3, 1), (3, 4, 1))
and so on. Please help me and sorry for not taking the time to post my
questions properly.
Victor
Or even so:
locals().update(("tuple_%s" % i, tuple((i,j,k) for j in range(1,5) for
k in range(1,2))) for i in range(1,5))
Ivan
Tried to make it readable:
def iter_coords(i):
for j in xrange(1,5):
for k in xrange(1,2):
yield i, j, k
def iter_vars():
for i in xrange(1, 5):
yield "tuple_%s" % i, tuple(iter_coords(i))
locals().update(dict(iter_vars()))
locals().update() works by accident here because it's in global scope;
it doesn't work within a function.
Use a proper data structure, like a dict or a list, and access each
tuple list as 'tuples[n]' instead of 'tuple_n'.
George
OP wanted variables and I showed him how to do this. I agree that a
list or a dict would be better.

Generating variable names at runtime doesn't work for locals and it is
a bad solution for globals in 99.9% of the cases. It is usually more
helpful to point someone who can't even express his problem clearly to
the right direction, rather than taking his pseudocode literally and
coming up with a semi-working translation.

George

Understanding of how to create variables dynamically can be good for
OP's learning curve even though it's a bad solution in this particular
case.

I agree that it was my mistake to not point him in the right
direction.

Ivan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top