0

I want to have a variable number of variables with the variables equal to an array of numbers. I know similar questions have been asked but none provided the answer I was seeking.I have tried having the variables top and bottom either tuples or numpy arrays but neither worked. I don't care what the type of numbers are because I can convert them later, I just need the variable names changed with each iteration.

i=[0] 
for x in i:
     top= (1,1,1)
     bottom= (1,1)

     top[i]=top
     bottom[i]=bottom
     i.append(x+1)          

Thanks for the help.

EDIT: Here is an example that probably better explains what I'm trying to do.

for x in range(1,4):
       top = (1,1,1)
       bottom = (1,1)

       if x == 1:
             top1 = top
             bottom1 = bottom
       if x == 2:
             top2 = top
             bottom2 = bottom
       if x == 3:
             top3 = top
             bottom3 = bottom

In the bottom code I'm creating a new variable with each iteration, but I'm only doing it for a set amount of iterations. How can I do it for an infinite amount of iterations?

faiz
  • 5
  • 4
  • You are using tuples, and not list. Try with [1,1,1]. Do you realize you are trying to make a recursive list? BTW what is the question? – Giacomo Catenazzi Mar 03 '18 at 06:46
  • Indexing a list with a list isn't going to work either. – hpaulj Mar 03 '18 at 06:58
  • @GiacomoCatenazzi I tried [1,1,1] and I got " TypeError: list indices must be integers or slices, not lists " . The question is how do I assign top to top1, top2, top3 as the iterations go by. The same for bottom. – faiz Mar 03 '18 at 06:59
  • "I want to have a variable number of variables" What are you asking here? "I have tried having the variables top and bottom ..." And what is this even supposed to mean? I can't understand your question as written. – AnOccasionalCashew Mar 03 '18 at 06:59
  • I know that the value of top and bottom do not change, so top1 is going to equal to top2 , top3 and so on. But I still want to learn how to name change through iteration. – faiz Mar 03 '18 at 07:02
  • I'm probably asking the question incorrectly but that's because I'm still new to coding. I'm trying to create new variables with each iteration. – faiz Mar 03 '18 at 07:04
  • I'm not sure I understand what you mean by "new variables" or "name change" [on each iteration]. Taken literally these things aren't possible, but perhaps you didn't mean what you (literally) wrote. – AnOccasionalCashew Mar 03 '18 at 07:08
  • Also, are you aware that you're probably going to get unexpected behavior due to modifying the list `i` [while iterating across it](https://stackoverflow.com/a/6260097)? As written isn't this an infinite loop due to the append on the last line? – AnOccasionalCashew Mar 03 '18 at 07:10
  • Sorry for the confusion. I hope the above edit helps. – faiz Mar 03 '18 at 07:13
  • Oh. Yeah, now that you edited to say "How can I do it for an infinite amount of iterations?" that's... an advanced topic. And **not advisable**. This is almost certainly not what you want to do. To learn more about it, see [this question](https://stackoverflow.com/q/5036700/) and [this one as well](https://stackoverflow.com/q/8028708/). – AnOccasionalCashew Mar 03 '18 at 07:16
  • You could probably accomplish your overall goal (whatever it is) via [dictionaries](https://docs.python.org/3/tutorial/datastructures.html#dictionaries), although I suspect this still isn't the best approach to whatever it is you're trying to do. – AnOccasionalCashew Mar 03 '18 at 07:20
  • Thanks I'll try it out. – faiz Mar 03 '18 at 07:25

1 Answers1

0

For the first example you provided, its behavior will most likely not be what you expected because you modify the list i while simultaneously iterating across it.

As for your second example, what you're asking is not advisable and almost certainly not what you actually want to do. See this question and this other one for more information.

Finally, I doubt this is actually the best approach for solving your problem, but you can accomplish (approximately) what you were trying to do in your question using a dictionary.

d = {}
v1 = (1,1,1)
v2 = (1,1)

for x in range(4):
    key1 = 'top' + str(x)
    d[key1] = v1
    key2 = 'bottom' + str(x)
    d[key2] = v2
AnOccasionalCashew
  • 601
  • 1
  • 5
  • 15
  • Thanks for your answers and effort. I thought it was more elegant to have an infinite loop with an eventual break, but it seems the non-infinite method is safer and easier to handle. Thanks again. – faiz Mar 03 '18 at 08:01
  • @faiz that depends entirely on what condition you'd like to break on. What did you have in mind? – AnOccasionalCashew Mar 03 '18 at 08:06