Are there any list comparison optimizations in Python?
Marco Antoniotti
marcoxa at cs.nyu.edu
Tue Nov 13 10:09:40 EST 2001
jbperez808 at yahoo.com (Jonathan P.) writes:
> >>> x=[1,2,3]
> >>> y=x
>
> >>> xlist=[x,1,2,3]
> >>> ylist=[y,1,2,3]
>
> >>> xlist==ylist
>
> >>> xlist.remove(x) # these 2 statements not executed one after
> >>> xlist.remove(y) # the other but representing different cases
> >>> ylist.remove(x)
>
> One of Python's most convenient high-level features is automatic list
> comparison by value and recursively.
>
> Does it know to optimize the compare if it has already been determined
> that two items share a reference to the same object by not doing a
> value compare on them anymore?
>
> And will this apply to both the comparison and remove() calls above?
Interesting concept. In Common Lisp you can program it in the
language. A first cut/draft could be.
(defun equal* (l1 l2)
(cond ((eql l1 l2) t)
((null l1) nil)
((null l2) nil)
((equal* (first l1) (first l2))
(equal* (rest l1) (rest l2)))))
Of course, there is nothing in the ANSI spec that forbids an
implmentor to build EQUAL and EQUALP in this way (actually I assume
this is the way things are built - have not checked though).
Cheers
--
Marco Antoniotti ========================================================
NYU Courant Bioinformatics Group tel. +1 - 212 - 998 3488
719 Broadway 12th Floor fax +1 - 212 - 995 4122
New York, NY 10003, USA http://bioinformatics.cat.nyu.edu
"Hello New York! We'll do what we can!"
Bill Murray in `Ghostbusters'.
More information about the Python-list
mailing list