Python 3.0 - is this true?

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sun Nov 9 02:32:59 EST 2008


On Sat, 08 Nov 2008 22:53:14 -0800, Kay Schluehr wrote:

>> How often do you care about equality ignoring order for lists
>> containing arbitrary, heterogeneous types?
> 
> A few times. Why do you care, Steven?

I'm a very caring kind of guy.


>> In any case, the above doesn't work now, since either L1 or L2 might
>> contain complex numbers.
>> The sorted() trick only works because you're making an assumption about
>> the kinds of things in the lists. If you want to be completely general,
>> the above solution isn't guaranteed to work.
> 
> You are right. I never used complex numbers in Python so problems were
> not visible. Otherwise the following comp function in Python 2.X does
> the job:
[...]
> Not sure how to transform it into a search key that is efficient and
> reliable.

Yes, that's a general problem. It's not always easy to convert a sort 
comparison function into a key-based sort. I know that 99% of the time 
key is the right way to do custom sorts, but what about the other 1%?


> [...]
> 
>> Here is a way to
>> solve the problem assuming only that the items support equality:
>>
>> def unordered_equals2(L1, L2):
>>     if len(L1) != len(L2):
>>         return False
>>     for item in L1:
>>         if L1.count(item) != L2.count(item):
>>             return False
>>     return True
> 
> Which is O(n**2) as you might have noted.

Yes, I did notice. If you can't assume even ordering, then you need to do 
a lot more work.


-- 
Steven



More information about the Python-list mailing list