[Tutor] trouble getting a list to update

Vincent Wan wan at walrus.us
Thu Jan 6 22:43:33 CET 2005


Dear All,

I am trying to learn python and an having a problem.

I wrote a program to repeatedly:
	a: print a list of integers eg 0 0 0 0 0 0 0 0 0 0
	b: change each integer in the list to 1 with a .05% chance

I run the program and over itterations more 1's appear as they should.

However, the changes printed in step a don't seem to be the same as  
those made in step b (as shown by the debug code.

Can someone tell me what I am doing wrong?

import random

total_time = 50
genome = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
mutation_rate = .05

def write_list(list_to_write, file_name):
     "Writes elements of a list, seperated by spaces, to a file"
     for each in list_to_write:
         file_name.write(str(list_to_write[each]) + ' ')
     file_name.write('\n')

def mutate_genome(a_genome, genome_length, rate):
     "Randomly changes a list ellement to 1"
     current_base = 0
     while current_base < genome_length:
         temp = random.random()
         myfile.write('base ' + str(current_base) + ' = ')        # debug
         myfile.write(str(a_genome[current_base]) + '\n')          #  
debug
         if temp <= rate:
             a_genome[current_base] = 1
             myfile.write('base ' + str(current_base) + ' mutated to ')   
       # debug
             myfile.write(str(a_genome[current_base]) + '\n')             
       # debug
         current_base = current_base + 1
     return a_genome


myfile = open('myfile', 'w')
current_time = 0
while current_time < total_time:
     myfile.write('\nTime is ' + str(current_time) + ' the genome is ')   
       # debug
     write_list(genome, myfile)
     length = len(genome)
     genome = mutate_genome(genome, length, mutation_rate)
     current_time = current_time + 1
myfile.close()

Thank you very much,

Vincent Wan

------------------------------------------------------------------------ 
--------------
PhD Candidate
Committee on the Conceptual and Historical Studies of Science
University of Chicago

PO Box 73727
Fairbanks, AK 99707

wan at walrus.us



More information about the Tutor mailing list