Is it possible to make a list with variables in it?

I'm making a quick simple harmonic oscillator program from an old Fortran prog to learn Python and I'd like to use a list to control output to a text file.

So far I have a list: writeLoop = [t, ' ', omega1, ' ', theta1, '\n'] ...computation...

for i in writeLoop
  textfile.write(str(i))

But all the output keeps on giving zeros as per the initial values of t, omega1 and theta1. It works when I output without using the list so the number crunching itself works well.

Is there an elegant way to use the current, newly calculated values of t, omega1, and theta1? Would there have to be some sort of pointer use? How would one program this in Python?

Thanks.

EDIT: I got it working by putting the list right before the for loop, but my original question still holds true for future reference.

for iter_var in iterable:

When iterating over iterable (a list, for example), the current element of the iterable is copied to iter_var, not referenced. This means that if that element of the iterable is altered in the current loop, iter_var will not change. If you want to it to change, use a pointer -- the index of the current value in the interable. (alist[index])

Copying

#Copying element to iter_var
alist = ['apple', 'orange', 'banana']
#enumerate returns tuple of (index, element) for each element, just so I can keep track of each element's index
for index, i in enumerate(alist):
    print index, i
    alist[index] = 'kiwi'
    print index, i
    print index, alist[index]
    #i hasn't changed, but the element of alist from which i came has)
    print '-' * 5
print alist

Output:

0 apple
0 apple
0 kiwi
-----
1 orange
1 orange
1 kiwi
-----
2 banana
2 banana
2 kiwi
-----
['kiwi', 'kiwi', 'kiwi']

Pointer

#Use index value as pointer to current element rather than copying to iter_var
alist = ['apple', 'orange', 'banana']
for index in range(len(alist)):
    print index, alist[index] 
    alist[index] = 'kiwi'
    print index, alist[index]
    print '-' * 5
print alist

Output

0 apple
0 kiwi
-----
1 orange
1 kiwi
-----
2 banana
2 kiwi
-----
['kiwi', 'kiwi', 'kiwi']

The only way that I know is to use a container. The list would only contain a reference to the container and so print the current value.

print "\n", "list test", "-"*50
t_list = [3]
omega_list = [0]
write_loop = [t_list, omega_list]
for j in range(0, len(write_loop)):
   print write_loop[j][0]
print "  After"
t_list[0] = 2
for j in range(0, len(write_loop)):
   print write_loop[j][0]


##=========================================================
print "\n", "dictionary test", "-"*45
write_loop = {}
write_loop[0] = 0
write_loop[1] = 1
for j in range(0, len(write_loop)):
   print write_loop[j]

print "  After"
write_loop[0] = 2
for j in range(0, len(write_loop)):
   print write_loop[j]


##=========================================================
class Test:
   def __init__(self):
      self.t = 0

print "\n", "class test", "-"*50
write_loop = [Test(), Test()]
for j in range(0, len(write_loop)):
   print write_loop[j].t

print "  After"
write_loop[1].t = 2
for j in range(0, len(write_loop)):
   print write_loop[j].t

Another way to print the variables is

textfile.write("%(t)s %(omega1)s %(theta1)s\n" % locals())
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.