Skip to content
Advertisement

This Python code gives unwanted output when query_words if of size greater than 1

I’ve written some code, but it does not output what I expected.

Here is the code:

JavaScript

The expected final value of big_ds is:

{123: {'dollar': ['currency'], 'probabilistic': []}, 108: {'dollar': [], 'probabilistic': ['probabilistic']}}

But the code sets the value of big_ds to the following:

{123: {'dollar': ['currency'], 'probabilistic': ['currency']}, 108: {'dollar': ['probabilistic'], 'probabilistic': ['probabilistic']}}

I asked a similar question a while back: How do I resolve this unexpected output in Python code? and was able to solve the issue for that use case. But that code I wrote fails when query_words has a size>1.

I can’t seem to figure out how to fix things. Any solution?

Advertisement

Answer

It’s because:

JavaScript

…the keys in each mail_id sub-dict each share the same list instance.

See:

Try this instead:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement