-4

I have to solve a problem (problem and the code is given below) and I'm getting errors on executing this program.

Traceback (most recent call last):
  File "C:/Python33/exercise1.py", line 9, in <module>
    for n in range[len(string) - k]:
TypeError: unsupported operand type(s) for -: 'int' and 'list'

-

Frequent Words Problem: Find the most frequent k-mers in a string.

Input: A string Text and an integer k. [XGMcKJXnoMhmuXcsjswaejVunrrsDhoiWEzXbiKoiYBEpVmhJszFWCFJAHLPzKfKBhWbCABPCTqASGvgquUtnwSeeYkXtLcbzMxvcsUwjmhHfexpEEhjhjzKvYdODZeCgrehxUnYqDwYMBxaFsYAqIFsBSZslMmTNXPovRtRbqFOhMXMUErCnRgjiBIovOWXxlkYInG]

Output: All most frequent k-mers in Text.

My designed code is:

k=open("dataset_3_6.txt").readlines()
import string
string = str()

for k in string:
  k = int(k)

kmer_count = {}
for n in range(len(string) - k):
  c = string[n:n+k]
  kmer_count[c] = kmer_count[c] + 1 if kmer_count.has_key(c) else 1

max = 0
max_kmer = []
for k,v in kmer_count.iteritems():
  if v > max:
    max_kmer = [k]
    max = v
  elif v == max:
    max_kmer += [k]

print("max_kmer")
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
evolvere
  • 101
  • 1
  • Please edit this code to make it legible. What are all the 'enter code here' blocks for? –  Nov 12 '13 at 04:15
  • The error means exactly what it says. What do you expect `len(string) - k` to evaluate to, and why? – Karl Knechtel Nov 12 '13 at 05:13

1 Answers1

3

There are a huge number of problems right at the top:

import string

Why are you importing this module? Are you planning to use it?

string = str()

This hides the string module so you can never use it again, and creates a default (that is, empty) string (which is easier to do with string = '').

for k in string:
    k = int(k)

Since string is an empty string, this loops zero times.

Even if it weren't empty, k = int(k) wouldn't have any effect; you're just rebinding the loop variable over and over again. If you wanted to replace the string string with a list of numbers, where each number is the integer value of the corresponding character in the string, you'd need to build a list, either by creating an empty list and calling append, or by using a list comprehension (e.g., string = [int(k) for k in string]). I have no idea whether that's what you're actually trying to do here.

Anyway, if string weren't empty, after the loop, k would be the int value of the last character in the string. But since it is, k is still the result of calling open("dataset_3_6.txt").readlines() earlier. That is, it's a list of lines. So, in this:

for n in range(len(string) - k):

You're trying to subtract that list of lines from the number 0. (Remember, you set string to an empty string, so its len is 0.)

I have no idea what you expected this to do.

Part of the confusion is that you have meaningless variable names, and you reuse the same names to refer to different things over and over. First k is the list of lines. Then it's a loop variable. Then… you apparently intended it to be something else, but I don't know what. Then, it's each key in a dictionary.

abarnert
  • 354,177
  • 51
  • 601
  • 671