python - Changing multiple positions in a list -
i have string want manipulate @ every position throughout it's length.
for example, have string looks this:
string1 = 'aatgcatt'
i want create list change 1 position @ time either a, t, c, or g. thinking of using approach so:
liststring = list(string1) changedstrings = [] pos in range(2, len(liststring) - 2): ##i don't want change first , last characters of string if liststring[pos] != 'a': liststring[pos] = 'a' random1 = "".join(liststring) changedstrings.append(random1)
i thinking of using similar approach append rest of changes (changing rest of positions g,c, , t) list generated above. however, when print changedstrings
list, appears code changes of positions after first two. wanted change position 3 'a' , append list. change position 4 'a' , append list , on give me list so:
changedstrings = ['aaagcatt', 'aatacatt', 'aatgaatt']
any suggestions?
you re-using old liststring
variable. initialize inside loop instead:
changedstrings = [] pos in range(2, len(liststring) - 2): liststring = list(string1) # move line here if liststring[pos] != 'a': liststring[pos] = 'a' random1 = "".join(liststring) changedstrings.append(random1) print(changedstrings)
results in
['aaagcatt', 'aatacatt', 'aatgaatt']
Comments
Post a Comment