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

4
  • 2
    I think that linked question is certainly an appropriate answer. Do yourself a favor and try that. Commented Feb 6, 2018 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. Commented Feb 6, 2018 at 15:40
  • 1
    You might have to build your own custom data structure that has a member for storing references to other ones Commented Feb 6, 2018 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. Commented Feb 6, 2018 at 15:48

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

That's a good idea. If there is no better way I think I will go with that one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.