Calculating similarity "score" between multiple dictionaries
<p>I have a reference dictionary, "dictA" and I need to compare it (calculate similarity between key and vules) to n amount of dictionaries that are generated on the spot. Each dictionary has the same length. Lets say for the sake of the discussion that the n amount of dictionaries to compare it with is 3: dictB, dictC, dictD.</p>

<p>Here is how dictA looks like:</p>

<pre><code>dictA={'1':"U", '2':"D", '3':"D", '4':"U", '5':"U",'6':"U"}
</code></pre>

<p>Here are how dictB,dictC and dictD look like:</p>

<pre><code>dictB={'1':"U", '2':"U", '3':"D", '4':"D", '5':"U",'6':"D"}
dictC={'1':"U", '2':"U", '3':"U", '4':"D", '5':"U",'6':"D"}
dictD={'1':"D", '2':"U", '3':"U", '4':"U", '5':"D",'6':"D"}
</code></pre>

<p>I have a solution, but just for the option of two dictionaries:</p>

<pre><code>sharedValue = set(dictA.items()) &amp; set(dictD.items())
dictLength = len(dictA)
scoreOfSimilarity = len(sharedValue)
similarity = scoreOfSimilarity/dictLength
</code></pre>

<p>My questions is:
How can I iterate through n amount of dictionaries with dictA being a primary dictionary which I compare others with. The goal is to get a "similarity" value for each dictionary I am gonna iterate through against the primary dictionary. </p>

<p>Thanks for your help.</p>

<p>Thanks to everyone for participation on the answer. Here is result that does what I need:</p>

<pre><code>def compareTwoDictionaries(self, absolute, reference, listOfDictionaries):
    #look only for absolute fit, yes or no
    if (absolute == True):
        similarity = reference == listOfDictionaries
    else:
        #return items that are the same between two dictionaries
        shared_items = set(reference.items()) &amp; set(listOfDictionaries.items())
        #return the length of the dictionary for further calculation of %
        dictLength = len(reference)
        #return the length of shared_items for further calculation of %
        scoreOfSimilarity = len(shared_items)
        #return final score: similarity
        similarity = scoreOfSimilarity/dictLength
    return similarity
</code></pre>

<p>Here is the call of the function</p>

<pre><code>for dict in victim_list:
                output = oandaConnectorCalls.compareTwoDictionaries(False, reference, dict)
</code></pre>

<p>"Reference" dict and "victim_list" dict are used as described above.</p>

python python-3.x