Lisp refactoring puzzle

Chris Kaynor ckaynor at zindagigames.com
Tue Jul 12 11:47:47 EDT 2011


On Mon, Jul 11, 2011 at 8:37 PM, Xah Lee <xahlee at gmail.com> wrote:
>
> it's funny, in all these supposedly modern high-level langs, they
> don't provide even simple list manipulation functions such as union,
> intersection, and the like. Not in perl, not in python, not in lisps.
> (sure, lib exists, but it's a ride in the wild)


Python has them, but, as they are set functions, not list functions, they
exist for the set type:

Intersection:
>>> set((1, 2, 3)) & set((2,3,4))
set([2, 3])

Union:
>>> set((1, 2, 3)) | set((2,3,4))
set([1, 2, 3, 4])

Symmetric Difference:
>>> set((1, 2, 3)) ^ set((2,3,4))
set([1, 4])


You can also get a non-symmetric difference by calling the difference method
of the set:
>>> set((1, 2, 3)).difference(set((2,3,4)))
set([1])
>>> set((2, 3, 4)).difference(set((1,2,3)))
set([4])
>>>

In Python 3 (2.7?) there is even more syntactical sugar for them: {1, 2, 3}
^ {2, 3, 4} produces {1, 4}.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110712/92362623/attachment-0001.html>


More information about the Python-list mailing list