Comparing three (or more) dictionaries and finding a match if at least two are equal

Alex Osheter

I'm faced with an issue similar to this one. However, that SO question is strictly focused on three variables. I am looking for a solution that would work for more than three as well.

Here's my code for two variables:

for track_a in collection_a:
    for track_b in collection_b:

        t1 = track_a["tempo"]
        t2 = track_b["tempo"]
        k1 = track_a["key"]
        k2 = track_b["key"]
        m1 = track_a["mode"]
        m2 = track_b["mode"]

        if (t1 == t2) and (k1 == k2) and (m1 == m2):
            collection_c.append((track_a, track_b))

Here's my solution for three variables:

for track_a in collection_a:
    for track_b in collection_b:
        for track_c in collection_c:

            t1 = track_a["tempo"]
            t2 = track_b["tempo"]
            t3 = track_c["tempo"]
            k1 = track_a["key"]
            k2 = track_b["key"]
            k3 = track_c["key"]
            m1 = track_a["mode"]
            m2 = track_b["mode"]
            m3 = track_c["mode"]

            a = (t1 == t2) and (k1 == k2) and (m1 == m2)
            b = (t2 == t3) and (k2 == k3) and (m2 == m3)
            c = (t3 == t1) and (k3 == k1) and (m3 == m1)

            if a: collection_c.append((track_a, track_b))
            if b: collection_c.append((track_b, track_c))
            if c: collection_c.append((track_c, track_a))

Obviously, this solution is not scalable and slow. Considering the fact I'd have to check all of them, I doubt it will ever be fast since we have to iterate over all possible combinations, but could I at least make it scale? (Up to at least 5). Also, if possible, allow more comparison characteristics to be added later.

blhsing

An efficient approach that solves the issue in linear time is to convert the dicts to frozen sets of key-value tuples (over keys that are used for equality tests) so that they can be hashable and used as dict keys (signatures) themselves, and so that you can simply use a dict of sets to group them:

groups = {}
for track in collections: # collections is a combination of all the collections you have
    groups.setdefault(frozenset((k, track[k]) for k in ('tempo', 'key', 'mode')), set()).add(track['name'])

so that:

[group for group in groups.values() if len(group) >= 3]

will return you a list of sets of names of the 3 tracks whose signatures are identical.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Comparing two dictionaries and checking how many (key, value) pairs are equal

Python: Comparing two dictionaries to see if two separate values match

Comparing Dictionaries and finding new objects

Comparing values of two dictionaries

Comparing two sorted dictionaries

comparing two files, if more than three lines are different then take action

Comparing two dictionaries and returning new dictionaries

Comparing two lists of dictionaries in Python

Comparing two arrays and finding differences

Finding averages using two dictionaries

Python comparing values from two dictionaries where keys match and one set of values is greater

Comparing two strings with equal(==) operator

Merge two dictionaries if the keys are equal

Comparing two columns in different file and printing match if difference between record in a columns less than or equal to 0.001

Comparing the values of two dictionaries using the IF condition

Python - comparing two dictionaries to create a new one

Comparing two dictionaries with numpy matrices as values

Comparing two [String: Any] dictionaries in Swift 4

Comparing python dictionaries and find diffrence of the two

Is there a way of comparing two dictionaries using the values of the keys?

Create third dictionary by comparing the values of two dictionaries

comparing order of values of two dictionaries with the same keys

Comparing data between two dictionaries in VBA

Comparing dates from two dictionaries with tuples as values

Finding centroid of two(or more) contours

comparing two lists and finding indices of changes

Comparing two images and finding the percent difference

Comparing two lists and finding indices of changes Dart

Comparing two files script and finding the unmatched data