I'm doing this switchboard thing in python where I need to keep track of who's talking to whom, so if Alice --> Bob, then that implies that Bob --> Alice.
Yes, I could populate two hash maps, but I'm wondering if anyone has an idea to do it with one.
Or suggest another data structure.
There are no multiple conversations. Let's say this is for a customer service call center, so when Alice dials into the switchboard, she's only going to talk to Bob. His replies also go only to her.
The kjbuckets C extension module provides a "graph" data structure which I believe gives you what you want.
You have two separate issues.
You have a "Conversation" object. It refers to two Persons. Since a Person can have multiple conversations, you have a many-to-many relationship.
You have a Map from Person to a list of Conversations. A Conversion will have a pair of Persons.
Do something like this
No, there is really no way to do this without creating two dictionaries. How would it be possible to implement this with just one dictionary while continuing to offer comparable performance?
You are better off creating a custom type that encapsulates two dictionaries and exposes the functionality you want.
You may be able to use a
DoubleDict
as shown in recipe 578224 on the Python Cookbook.You can create your own dictionary type by subclassing
dict
and adding the logic that you want. Here's a basic example:And it works like so:
I'm sure I didn't cover all the cases, but that should get you started.
In your special case you can store both in one dictionary:
Since what you are describing is a symmetric relationship.
A -> B => B -> A