0

When I submit the following code:

data = open("DNA Motif.txt", "r")
lines = data.readlines()
large = list(lines[0])
small = list(lines[1])
list = []
counts = []

print large

for first in range(0, len(large)):
    list = []
    if large[first] == small[0]:
        for other in range(0, len(small)):
            if small[other] == large[first + other]:
                list.append(large[first + other])
            if list == small:
                counts.append(str(first + 1))

With the following data set:

TTGCGTTTCGCGTTTCTCCGTTGCGTTTCTAGCGTTTCCAGGCGACCGCGTTTCGCGTTTCAGCGTTTCGCGTTTCGCGTTTCGCGTTTCCCCGGGGGCGTTTCGCGTTTCGGCGTTTCCAGCGTTTCCGCGTTTCGCGTTTCGCGTTTCAGCATAGTTAGCGTTTCATCGCGTTTCGCGTTTCTTTTATGCTGTTGCGTTTCTGCTTAGGCGTTTCTACACTCAGCGTTTCGCGTTTCAGCGTTTCAGCGTTTCACAGCGTTTCGCGTTTCTGGCGTTTCGCCGCGTTTCCCGTGCGTTTCATTCTTGCGTTTCTGCGTTTCTCGAGAGCGTTTCGCGTTTCGTACGCGTTTCGCGTTTCTTAAGAGCGTTTCCGCGTTTCGTTACGCGTTGCAAGCGTTTCGGCGTTTCTCAGGCGTTTCGTGCGTTTCTGCGTTTCGCGTTTCTTTGCGTTTCGCGTTTCGAAAGCGTTTCGGTTGCCTACGTATATACTTGCGTTTCGCGTTTCCTTCTAAAGGCGTTTCATAGCGTTTCGCCCTGGAGCGTTTCCGGAGAGCGTTTCTGCGTTTCAGCGTTTCTGCGTTTCAGGAGCGTTTCGGTGAGATTTAGCGTTTCCTATCGGCGTTTCTGGCGTTTCCTCGAAAGCGTTTCAGGCGTTTCATGCGTTTCTGCGTTTCGCGTTTCTCCGGCGTTTCGGCGTTTCGGCGTTTCGCGTTTCAGCGTTTCCAGCGTTTCAAGCGGCGTTTCACGCGTTTCGCGTTTCGCGTTTCGCGTTTCACTGCGTTTCAAGCGTTTCGCGTTTCGCGTTTCAACGCTGCGTTTCAGCGTTTCGCGTTTCGCGTTTCTGACCCGCGTTTCAGCGTTTCTGGGATAGCGTTTCGTGCGTTTCGCGTTTCGCGGTGACGCGTTTC
GCGTTTCGC

I get this error:

IndexError: list index out of range

If you could help that would be great. I understand what the error is, and how I should fix it, but I don't know specifically what to fix in my code.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • **Where** does the error occur? What is the *full* traceback of the exception? – Martijn Pieters Nov 30 '14 at 03:38
  • And what if `first` is set to `len(large) - 1` (so the last available index in `large`)? You now try to add `other` to that, and for any value other than `0` for `other`, that'll give you an exception. The same will happen for any value starting at `len(large) - len(small)`, as you are guaranteed to generate values for `other` that'll exceed `len(large) - 1` when summing. What are you actually trying to do here? – Martijn Pieters Nov 30 '14 at 03:40
  • The error occurs on line 14: 'if small[other] == large[first + other]:' – Thomas Williams Nov 30 '14 at 03:40
  • @MartijnPieters Thanks, I think that fixed it. I'm trying to find out how many times "small" occurs in "large" – Thomas Williams Nov 30 '14 at 03:42
  • Like [Find all occurrences of a substring in Python](http://stackoverflow.com/q/4664850) you mean? – Martijn Pieters Nov 30 '14 at 03:44
  • Martijn likely solved your problem. Just as a note- you shouldn't be going from 0 to len(large)... you should be going from 0 to (len(large)-len(small) ... this is the cause of your actual error. – Paul Becotte Nov 30 '14 at 03:46

1 Answers1

0
for first in range(0, len(large)):                       #1
    list = []
    if large[first] == small[0]:
        for other in range(0, len(small)):               #2
            if small[other] == large[first + other]:     #3
  1. The largest value first will be is len(large) - 1.

  2. The largest value other will be is len(small) - 1.

  3. Thus, the largest value that first + other will be is len(large) + len(small) - 2.

Given that len(small) is 9 from your example input, first + other is clearly going to be larger than len(large) at some point, and yet you're trying to use it as an index for large.

You need to fix your logic - or, as Martijn Pieters pointed out, use a simpler method.

Community
  • 1
  • 1
Amber
  • 507,862
  • 82
  • 626
  • 550