0

I've created 28 char array seq, and copied it 100 times to other array copies.

Now at each copy I'd like to change one random char to the other one - def mutate(). It works when it is not in "for". When I try to do it for every copy (for i in range (0, 99) ) it changes copy not only at "i" index but every copy (index) in 2D array "copies".

Why it works like this or how to change it? Why it doesn't change only at current index "i"?

import random

    def rand():
        a = 'ABCDEFGHIJKLMANOPQRSTUWVYZ '
        alphabet = list(a)
        seq = []
        for i in range (0, 27):
            seq.append(alphabet[random.randint(0,26)])
        copies = []
        for i in range (0, 99):
            copies.append(seq)
        #print (alphabet)
        print(copies[10])
        s="".join(seq)
        return copies, alphabet


    def mutate(copies, alphabet):

        print(copies[10])
        for i in range(0, 99):
            tmp = random.randint(0,26)
            copies[i][tmp]=alphabet[random.randint(0,26)]
            print(copies[10])
        print(copies[10])


    copies, alphabet = rand()
    print(len(copies))
    #print(copies[10])
    #print(copies[11])
    mutate(copies, alphabet)
    print(len(copies))
jignesh Vadadoriya
  • 3,244
  • 3
  • 18
  • 29
Tomek
  • 13
  • 1
  • 3

0 Answers0