Quantcast
Viewing all articles
Browse latest Browse all 14040

How can I convert a dictionary with tuple ids into a dictionary of dictionaries mapping id to all other ids?

In python, I'm trying to convert a dictionary into a dictionary of dictionaries. The keys of dictionary 1 are tuples of id pairs. The value associated with this pair is a float that measures the similarity between those two items.

What I want is dictionary 2, where the key is a single ID, and the value is another dictionary with single key:value pairs of all the other IDs that are paired with that first ID (no matter the id order). See below for an example:

Dict1 = { (1, 2): 0.2, (1, 3): 0.4, (1, 4): 0.7, (2, 3): 0.25, (2, 4): 0.5 }

Dict2 = { 1: {2: 0.2, 3: 0.4, 4: 0.7}, 2: {1: 0.2, 3: 0.25, 4: 0.5} }

How can I manage this in the most computationally efficient way?

I tried nested for loops, but can't quite figure out how to identify the unique id pairings associated with a particular id. I also don't think that would be the most computationally efficient way and am wondering if there's a way to do it without nested loops.


Viewing all articles
Browse latest Browse all 14040

Trending Articles