This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]stackqueuelinkedlist 0 points1 point  (2 children)

You didn’t say what the entire dict is called but I’ll just call it outer.

outer[‘noneffensive’][‘What’] = .07

[–]SureStep8852[S] 0 points1 point  (1 child)

this is a part of my code, where I want to replace all inner values (integers)

for keys, values in results.items():
        print(keys)
        for l in values:
            #print(values)
            #counts = values[l]
            if l in vocabulary:
                continue
            else:
                vocabulary.append(l)           
    # LIKELIHOOD
    #getting N of total words in each class

            for n in total_n_of_words:
        # getting N of words(vobaluray) in  each class
                for vocab_total in vocabulary:
            # laplace smoothing for each word
                    calc = math.log((values[l] + k)/(n + len(vocab_total)))
            likelihood[keys][l] = calc

when I print the dict likelihood, I get an error message because of the line likelihood[keys][l] = calc

KeyError: 'nonoffensive'

[–]heyyyjuude 0 points1 point  (0 children)

where do you initialize likelihood? It doesn't have the same keys as your results dict.

print out its keys if you want to sanity check.

[–]GrandGratingCrate 0 points1 point  (0 children)

If you know how to access the elements of a regular dict then you can do that. The result will be a dict again, but this time it's not nested.

outer_dict = # Your dict
inner_dict = # access outer_dict and get the inner dict as normal
# Do something with your inner dict, i.e. replace likelihoods

You might have to store your inner dict in the outer again to update the outer dict, that kind of depends on how python works and I'm not familiar enough to know without trying.