1

Suppose I have an object of the structure:

class Obj:
  def match(other):
    return check_match(self, other)

For example, check_match could check for overlap or for equality or for "is friend".

Now I'll have a list of those objects:

objList = create_random_list_of_Obj()

Now, for each element which matches other elements, I d like to keep only one of those mutually matching elements.

Remark and clarification: So far, the task is ambiguous: If A and B match, and B and C match, but A and C do not match, then we could keep A and C or we could keep B. It's not important, which decision the algorithm will take. The important thing is, that there are no matching elements in the final list.

Two questions:

  • What is the pythonic way to do so?
  • Does this problem have a name?
Michael
  • 7,407
  • 8
  • 41
  • 84

2 Answers2

3

Regarding: What is the pythonic way to do so?

The pythonic way is to rename your match method to __eq__.

Then you can do things like element in list with your custom object.

A very pythonic way to make a list contain unique items is then:

uniques = list(set(list_with_duplicates))

Edit: __ne__ is not needed and will default to not __eq__()

Does this problem have a name?

One could look for "equivalence classes".

Some references:

Community
  • 1
  • 1
Robin Roth
  • 1,289
  • 10
  • 13
  • The idea with the set seems smart, at least for transitive a `match` (equality for example). I wonder what will happen, if `match` is not transitive: "is friend" for example. A can B friend with B, B can be friend with C, but A is not necessarily friend with C. – Michael Jul 06 '16 at 07:53
  • 1
    @Michael The setup with set and `__eq__` still works, it would make sure that you only have one of {A,B} and one of {B,C}, but would allow both {A,C}. The outcome is not deterministic then, since if you start with {A,B,C} you can end with {A,C} or {B}. But on the other hand your questions is not deterministic if "is friend" is not transitive. – Robin Roth Jul 06 '16 at 08:22
  • I'm aware that the solution to the given task is not unique. Application will be the removal of overlapping objects (not transitive) as well as removal of equal elements in a list (transitive). The only constraint in both cases is the same: Have no matching pairs in the final list. But I see that the non-transitive case could be extended to a difficult task when more constraints would apply ("keep as many as possible", "loose no matching group"). Full-fledged graph theory problems... – Michael Jul 06 '16 at 08:33
-2

Python has a type of contains method that works with the following syntax: if x in y: Use it to determine if a list contains an obj

a = ["a", "b", "c", "d", "d"]
b = ["c", "c", "d", "e", "f"]
c = []

for member in a:
  if member in b and member not in c:
    c.append(member)

Should result in c = ["c", "d"]

xandermonkey
  • 4,054
  • 2
  • 31
  • 53
mstagg
  • 507
  • 4
  • 16
  • 1
    The way that you have interpreted this problem, there are better solutions. For instance, using set intersections, or a direct list comprehension. – miradulo Jul 05 '16 at 16:32