Levon Asatryan
Levon Asatryan

Reputation: 195

Issue with python __eq__ method in checking if 2 lists are equal

I have a python program in which I have a class called Vector and an empty list inside of that class which is being populated runtime. Here is the init:

def __init__(self,n):
    self.vector = [];
    self.n = n;
    for x in range(n):
        self.vector.append(False);

And here is the eq:

def __eq__(self, other):
    t = True
    for x in range(self.n):
        if self.vector[x] != other.vector[x]:
            t = False;
    return t

however, when I try to check if 2 objects of this type are equal, I always get true, even though I changed values inside of vector in Vector class. Here is the code where I do the above:

vectors = []
    n = tmp.size();
    k = calculateCombinationCount(n,int(n/2))
    for i in range(k):
        for j in range(0,n-1):
            if (tmp.vector[j] != tmp.vector[j+1]):  
                t = True
                for x in vectors:
                    if x == tmp:
                        t = False;
                if t:
                    vectors.append(tmp)
                    tmp.printVector();
                tmp.swap(j,j+1);

I would appreciate any help that you can provide. Thank you :)

EDIT:

def swap(self,i,j):
    tmp = self.vector[i]
    self.vector[i] = self.vector[j]
    self.vector[j] = tmp

def calculateCombinationCount(n,r):
k = factorial(n)/(factorial(int(r))*factorial(int(n-r)))
return int(k)

Upvotes: 2

Views: 743

Answers (1)

FHTMitchell
FHTMitchell

Reputation: 12157

Right so I've updated your code to be much more pythonic (I can tell you come from another language, Java?).

from math import factorial

class Vector:

    def __init__(self, size):
        self.size = size
        self.vector = [False] * size

    def __eq__(self, other):
        """
        Same if self.size == other.size
        """
        assert self.size == other.size, (self.size, other.size)
        return self.vector == other.vector

    def print_vector(self):
        print(self.vector)

    def swap(self, i, j):
        """
        More efficient and pythonic
        """
        self.vector[i], self.vector[j] = self.vector[j], self.vector[i]


def calculate_combination_count(n, r):
    """
    This is slow, I'd replace it with scipy.special.comb
    https://docs.scipy.org/doc/scipy/reference/generated/scipy.special.comb.html#scipy.special.comb
    """
    return factorial(n) // (factorial(r) * factorial(n-r))


tmp = Vector(10)

vectors = []
n = tmp.size
k = calculate_combination_count(n, n // 2)
for i in range(k):
    for j in range(0, n-1):
        if tmp.vector[j] != tmp.vector[j + 1]:
            if not any(vec == tmp for vec in vectors):  # much more efficient
                vectors.append(tmp)
                tmp.print_vector()
            tmp.swap(j, j + 1)
        else:  # Just to prove why it doesn't work
            print('tmp.vector is all False: {}'.format(not all(tmp.vector)))

This prints out tmp.vector is all False: True repeatedly. I think this is your problem.

If you

Upvotes: 1

Related Questions