I'm trying to record the number of repetitions of each str in the sequence. I created a dictionary as below and another empty dict because i couldn't update a dict while iterating through it. But for some reason my value variable always returns 0 for every str. Any suggestions?

iterator = {     
    "AGATC": 0,
    "TTTTTTCT": 0,
    "AATG": 0,
    "TCTAG": 0,
    "GATA": 0,
    "TATC": 0,
    "GAAA": 0,
    "TCTG": 0

record = {

for x in iterator:
    value = 0
    for i in range(0, len(sequence)):
        if sequence[i:len(x)] == x:
            value += 1
        elif sequence[i:len(x)] != x:
            value = 0
    record.update( {x : value} )   # wordFreqDic.update( {'before' : 23} )


This is how it prints :

~/pset6/dna/ $ python dna.py databases/large.csv sequences/1.txt
{'AGATC': 0, 'TTTTTTCT': 0, 'AATG': 0, 'TCTAG': 0, 'GATA': 0, 'TATC': 0, 'GAAA': 0, 'TCTG': 0}

Consider this snippet:

x = "AGATC"
i = 2
print(sequence[i:len(x)])     # this is your code
print(sequence[i:i+len(x)])   # this is probably what you want

It prints:


More information on indexing and slicing can be found here.

Also interesting: sequence.count(x)

  • 1
    Hey @Marijn, I understand the bug you're trying to solve with the snippet print(sequence[i:i+len(x)]) and while that is something I have to change (thanks !), the values still return zero, so it has to be something else. I've spent so much time on this one trying to figure it out but man this is hard. I've also tried sequence.count(x) but that returns the total number of repetitions of the substring while what we want is the highest number of consecutive repetitions. Anyways, appreciate your effort man :) – zeek Aug 30 '20 at 4:53
  • Yes, your next problem has to do with the elif sequence[i: _i+_len(x)] != x: condition. That will be true for the last i, when it indexes the last character in the array and then value will be set to 0 ... probably not what you mean to do. – Marijn Aug 30 '20 at 6:14
  • And when you have fixed that, your code will not return the highest number of repetitions yet, I think it will count quite similar to sequence.count. – Marijn Aug 30 '20 at 6:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .