LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Assigning names in Python (https://www.linuxquestions.org/questions/programming-9/assigning-names-in-python-347260/)

DiagonalArg 07-27-2005 08:05 AM

Assigning names in Python
 
Hi. I'm having a problem with a Python program I'm trying to write, and I was wondering if any of you could help. I guess it's a pretty elementary question, but I'm a programming newbie, so there you go.

I have a for loop in my program and I would like on each iteration to assign a name. So, for example, the first time through I want the statement x1=some_function(1), the second x2=some_function(2) etc. I realise that I can't just write

for i in range(y):
xi=some_function(i)

as that will just keep assigning xi. So I'd really like to be able to write something like

for i in range(y):
'x'+str(i)=some_function(i)

but that that's not allowed. What's the best way round this problem?

Proud 07-27-2005 08:23 AM

Why not use an array? Then you'd have your data stored at x[0], x[1] etc. :)

use x[i] instead of xi.

DiagonalArg 07-27-2005 08:51 AM

Yeah, I realised an array was the way to go about 2 minutes after posting this. D'oh. :rolleyes:

Crashed_Again 07-27-2005 03:09 PM

More specifically use a list which, as Proud pointed out, is denoted by the [ and ]. Tuples could also be considered arrays but since they are immutable they wouldn't work in this situation.


All times are GMT -5. The time now is 12:31 AM.