0

I have a string that holds nucleotide values. I want to mark all nucleotides on this string with 0 and 1. Here the A G C T values can be in any position.

for example

str = 'AGCTAGG' 

and I want to keep an extra value on this string that I can change later.

initially 0 assigned to all values:

new-str = {A:0,G:0,C:0,T:0,A:0,G:0,G:0}

Later I want to assign 1 to nucleotide that I want:

new-str = {A:1,G:0,C:1,T:0,A:0,G:1,G:0}

The letters will be kept in the order in the string.

What is the best method that I can use here?

Also the defaultdict didn't give me what I wanted.

dict
defaultdict
hashmap
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
neodeep
  • 15
  • 5
  • Does this answer your question? [Iterating each character in a string using Python](https://stackoverflow.com/questions/538346/iterating-each-character-in-a-string-using-python) – Sorceri Dec 19 '22 at 21:40

1 Answers1

1

Put the marks in a separate list of the same length as the string. If you want to keep the marks and the string together, you could make them into an object, or with less boilerplate, a namedtuple (q.v.), but essentially you want them to be two separate things.

If you then want to iterate over genes with marks, use zip:

for gn, mk in zip(genes, marks):
    # do something with gn amd mk

Personally, I'd use True and False rather than 0 or 1. But that's a minor detail.

rici
  • 234,347
  • 28
  • 237
  • 341