test for list equality

Ian Kelly ian.g.kelly at gmail.com
Fri Dec 16 02:32:11 EST 2011


On Fri, Dec 16, 2011 at 12:11 AM, Ian Kelly <ian.g.kelly at gmail.com> wrote:
> On Thu, Dec 15, 2011 at 11:30 PM, Alec Taylor <alec.taylor6 at gmail.com> wrote:
>> Just for fun, use the Hungarian Algorithm
>>
>> (Python implementation: http://software.clapper.org/munkres/)
>
> That's a pretty silly approach, but okay:
>
> def listequals(a, b):
>    if len(a) != len(b):
>        return False
>    matrix = [[int(item_a != item_b) for item_b in b] for item_a in a]
>    path = Munkres().compute(matrix)
>    return sum(matrix[r][c] for (r, c) in path) == 0

Amendment -- it seems that Hungarian implementation fails on an empty matrix:

def listequals(a, b):
    if len(a) == len(b) == 0:
        return True
    if len(a) != len(b):
        return False
    matrix = [[int(item_a != item_b) for item_b in b] for item_a in a]
    path = Munkres().compute(matrix)
    return sum(matrix[r][c] for (r, c) in path) == 0



More information about the Python-list mailing list