2

I have a string that I want to manipulate at every position throughout it's length.

For example, I have a string that looks something like this:

string1 = 'AATGCATT'

I want to create a list where I change one position at a time to either an A, T, C, or G. I was thinking of using an approach as so:

liststring = list(string1)
changedstrings = []
for pos in range(2, len(liststring) - 2): ##I don't want to change the first and last to characters of the string
    if liststring[pos] != 'A':
        liststring[pos] = 'A'
        random1 = "".join(liststring)
        changedstrings.append(random1)

I was thinking of using the similar approach to append the rest of the changes (changing the rest of the positions to G,C, and T) to the list generated above. However, when I print the changedstrings list, it appears the this code changes all of the positions to A after the first two. I really just wanted to change position 3 to 'A' and append to list. Then change position 4 to 'A' and append to list and so on to give me a list as so:

changedstrings = ['AAAGCATT', 'AATACATT', 'AATGAATT'] 

Any suggestions?

Selcuk
  • 57,004
  • 12
  • 102
  • 110
interstellar
  • 389
  • 5
  • 13
  • 1
    It's not clear what you mean by "all possible variations of this string in regards to A,T,C, and G's". This isn't helped by the later description. Exactly what combinations/permutations are you trying to generate? – jpmc26 Mar 07 '16 at 01:57
  • Possible duplicate of [How to generate all permutations of a list in Python](http://stackoverflow.com/questions/104420/how-to-generate-all-permutations-of-a-list-in-python) – intboolstring Mar 07 '16 at 01:59
  • 1
    @jpmc26 working on a clarification – interstellar Mar 07 '16 at 01:59

2 Answers2

2

You are re-using the old liststring variable. Initialize it inside the loop instead:

changedstrings = []
for pos in range(2, len(liststring) - 2):
    liststring = list(string1)  # Move this line here
    if liststring[pos] != 'A':
        liststring[pos] = 'A'
        random1 = "".join(liststring)
        changedstrings.append(random1)
print(changedstrings)

results in

['AAAGCATT', 'AATACATT', 'AATGAATT']
Selcuk
  • 57,004
  • 12
  • 102
  • 110
0

From what I understand of your question, you need two loops. One for the positions (all except the first and the last), and another one for the four letters (nucleic acids?) A,T,G,C. You can decide which loop you place first.

To change all positions except the first and the last, it should be for pos in range( 1, len(liststring)-1):. Look in the documentation for the range function. Also have a look at how indices work in python.

string1 = 'AATGCATT'
changedstrings = []
for new_char in ('A','T','G','C'):
  for pos in range( 1, len(string1)-1 ):
    if string1[pos] != new_char:
      liststring = list(string1)
      liststring[pos] = new_char
      changedstrings.append("".join(liststring))
print (changedstrings)
Sci Prog
  • 2,651
  • 1
  • 10
  • 18