Why do I get this error while trying to modify a dictionary: "TypeError: unhashable type: 'slice'

StackOverflow https://stackoverflow.com/questions/23635432

سؤال

I'm trying to build a function which gets as an input a string and a number k, and returns a dictionary whose keys are the string of length k from the original string and the value for each key is a sequence of the indexes at which the string appeared.

Running example :

<<< hash_sequence("byebyeboy",3 ( 

{ 'yeb': [1, 4], 'boy': [6], 'bye': [0, 3], 'ebo': [5], 'eby': 
[2] } 

This is my code:

def hash_sequence(string, k):
    dictionary={}
    for i in range(len(string)):
        dictionary[i:i+3]=[i]
        if dictionary[i:i+3] in dictionary:
            dictionary[i:i+3]+=[1]
    return dictionary 
هل كانت مفيدة؟

المحلول

Oh, I see what you're trying to do - slice the string. Wrong syntax for that.

dictionary[i:i+3]=[i]

Should be

dictionary[string[i:i+3]]= [i]

Likewise if dictionary[i:i+3] in dictionary: becomes if string[i:i+3] in dictionary: and so forth on the next line.

Honestly the way you're doing this makes the code very confusing. Things are clearer if you use setdefault (and fix a couple of other bugs):

def hash_sequence(s, k=3):
    dictionary={}
    for i in range(len(s) - k + 1):
        triplet = s[i:i+k]
        dictionary.setdefault(triplet,[]).append(i)
    return dictionary

hash_sequence('byebyeboy')
Out[28]: {'boy': [6], 'bye': [0, 3], 'ebo': [5], 'eby': [2], 'yeb': [1, 4]}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top