0

I was wondering how I could implement a many-to-many relationship data-structure. Or if something like this already exists in the wild.

What I would need is two groups of objects, where members from one group are relating to multiple members of the other group. And vice versa. I also need the structure to have some sort of consistency, meaning members without any connections are dropped, or basically cannot exist.

I have seen this answer (it involves SQL-lite database), but I am not working with such huge volumes of objects, so it's not an appropriate answer for this context Many-to-many data structure in Python

SirSteel
  • 123
  • 1
  • 10
  • 2
    I think [that linked question](https://stackoverflow.com/questions/3538322/many-to-many-data-structure-in-python) is certainly an appropriate answer. Do yourself a favor and try that. – Edwin van Mierlo Feb 06 '18 at 15:36
  • I am looking for something simpler. Since this would usually be a 10 to 10 relation, and a 50 to 50 at the most pessimistic extreme. Making an SQL database, seems quite an overhead. But, thanks for the input. – SirSteel Feb 06 '18 at 15:40
  • 1
    You might have to build your own custom data structure that has a member for storing references to other ones – Gillespie Feb 06 '18 at 15:42
  • I see. Yeah, was just wondering if something like this already exists or if there is a common pattern to use. I could do it like Anne recommended using a numpy array. – SirSteel Feb 06 '18 at 15:48

1 Answers1

1

Depending on how big your dataset is, you could simply build all possible sets and then assign booleans to see whether the relationship exists.

itertools.combinations

can be of help to generate all possible combinations. Consistency can then be added by checking if any connections are True for each value.
I do not claim this is the prettiest approach, but it should work on smaller datasets.

https://docs.python.org/2/library/itertools.html#itertools.combinations

Anne
  • 583
  • 5
  • 15