-3

I have a tuple of tuples in the form:

test=((-1.0, 1.0, 0.0),
    (-1.0, -1.0, 0.0),
    (1.0, -1.0, 0.0),
    (1.0, 1.0, 0.0))

I would like to name each tuple in a loop as:

Desired Output:

coord1 = (-1.0, 1.0, 0.0)
coord2 = (-1.0, -1.0, 0.0)
coord3 = (1.0, -1.0, 0.0)
coord4 = (1.0, 1.0, 0.0)

Not sure how to implement this for a variable amount of assignments. i.e., my tuple of tuples has a length of 604.

I'mahdi
  • 23,382
  • 5
  • 22
  • 30
shoggananna
  • 545
  • 5
  • 9
  • `coord1 = test[0]`... for a fixed number of variables / assignments. For a variable amount of variables you should not do this, a collection like tuple or list is the proper data representation. – luk2302 Jun 25 '22 at 12:33
  • Do you mean something like dynamic variable creation? Because what you present is actually the assignment of each of the tuples to a new variable. – deponovo Jun 25 '22 at 12:34
  • 2
    Does this answer your question? [How do I create variable variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-variable-variables) – Maurice Meyer Jun 25 '22 at 12:36
  • Hi. I updated my post. So yes, it would need to be dynamic variable creation. – shoggananna Jun 25 '22 at 12:36
  • 1
    You won't be able to generate code variables dynamically. You could use a dictionary as Sash and I proposed, and then use the keys as your pseudo dynamic variables. – deponovo Jun 25 '22 at 12:40

3 Answers3

1

The question is ambiguous. What exactly is assigning a name? Mapping each of the tuples to a given string? Then you could map the tuples via a dictionary as:

{f'coord{x}': tup for x, tup in enumerate(test)}
deponovo
  • 1,114
  • 7
  • 23
1

You can try this for more coord as you say you have 604 coords:

test=((-1.0, 1.0, 0.0),(-1.0, 1.0, 0.0),
    (-1.0, -1.0, 0.0),(-1.0, -1.0, 0.0),
    (1.0, -1.0, 0.0),(1.0, -1.0, 0.0),
    (1.0, 1.0, 0.0),(1.0, 1.0, 0.0))

coors = map(lambda x: f'Coords {x}', range(len(test)))

res = zip(coors, test)

print(*(map(lambda x: f'{x[0]} = {x[1]}', res)), sep = '\n')

Output:

Coords 0 = (-1.0, 1.0, 0.0)
Coords 1 = (-1.0, 1.0, 0.0)
Coords 2 = (-1.0, -1.0, 0.0)
Coords 3 = (-1.0, -1.0, 0.0)
Coords 4 = (1.0, -1.0, 0.0)
Coords 5 = (1.0, -1.0, 0.0)
Coords 6 = (1.0, 1.0, 0.0)
Coords 7 = (1.0, 1.0, 0.0)

Edit base comments:

test=((-1.0, 1.0, 0.0),(-1.0, 1.0, 0.0),
    (-1.0, -1.0, 0.0),(-1.0, -1.0, 0.0),
    (1.0, -1.0, 0.0),(1.0, -1.0, 0.0),
    (1.0, 1.0, 0.0),(1.0, 1.0, 0.0))

coors = list(map(lambda x: f'Coords_{x}', range(len(test))))


for i in range(len(coors)):
    globals()[coors[i]] = test[i]
    
print(Coords_0)
print(Coords_5)

Output:

(-1.0, 1.0, 0.0)
(1.0, -1.0, 0.0)
I'mahdi
  • 23,382
  • 5
  • 22
  • 30
  • @l'mahdi: I would like to accept this answer as I like the lambda function usage. Thanks everyone for your input. – shoggananna Jun 25 '22 at 12:50
  • 1
    This answer is legitimate and actually ok. But one thing I don't like about it is that it is like the same with extra steps. Many python programmers catch these "cool" implementations and then go around complaining about python being so slow. As I wrote, the answer is ok, I would just not suggest such solution for the problem at hands. – deponovo Jun 25 '22 at 12:53
  • @I'mahdi: How would I assign this output result to a variable instead of using print? – shoggananna Jun 25 '22 at 15:31
  • @user9106985, do you want this : `dict(zip(coors, test))` – I'mahdi Jun 25 '22 at 15:32
  • not a dictionary but a list preferably. I tried res1 = list(zip(coors,test)) but it gives me Coords 0 as 'Coords 0' ie inside qoutes – shoggananna Jun 25 '22 at 15:42
  • can you send me an example, what do you want exactly? – I'mahdi Jun 25 '22 at 15:43
  • essentially, I need to export exactly what is shown in the output of your solution to a external program. – shoggananna Jun 25 '22 at 15:51
  • 1
    @user9106985, edited. Are you wanting this? – I'mahdi Jun 25 '22 at 16:13
-1

Create each tuple into a key-value pair using dictionary:

test = ((-1.0, 1.0, 0.0),
        (-1.0, -1.0, 0.0),
        (1.0, -1.0, 0.0),
        (1.0, 1.0, 0.0))

coords = dict()

for i in range(len(test)):
    coord = test[i]
    coords[f"coord {i + 1}"] = coord

print(coords)

Sidharth Mudgil
  • 1,293
  • 8
  • 25