beginner, idiomatic python
david at asdf.asdf
Mon Aug 27 03:54:37 CEST 2007
c = sorted(set(a)-set(b))
although for me :~( that is another step more obscure than
c = list(set(a)-set(b))
Bags don't seem to be built in to my copy of Python, and
although I'm interested in why lists don't support the difference
operation, I don't want to get away from standard Python.
"Scott David Daniels" <daniels at dsl-only.net> wrote in message
news:13d00hu2mflt01b at corp.supernews.com...
> bambam wrote:
>>> The reason that lists don't have set-like methods is because
>>> lists aren't sets -- lists can contain duplicate elements
> and they are ordered. I'd have used sets if I was sure you
> meant [1,2,3] to mean the same thing as [3,1,2] and no duplicates.
>> Interesting point -- if that's all there is in it, then lists should
>> have difference and intersection methods. Not because they
>> are the same as sets -- because they are slightly different than
>> sets. In this case it doesn't matter - my lists don't contain
>> duplicate elements this time - but I have worked with lists in
>> money market and in inventory, and finding the intersection
>> and difference for matching off and netting out are standard
> Here you seem to be talking about multisets (also called bags).
> They have more fully defined algebraic properties analogous to sets.
> bag([1,2,3,3,4]) == bag([3,1,2,4,3]) != bag([1,2,3,4])
> bag([1,2,2,3]) - bag([1,2]) == bag([2,3])
> bag([1,2,3]) - bag([3,4]) == bag()
>>>> Excellent. By symmetry, I see that "list" casts the set back into a
> Some will say 'sorted' is a better conversion of a set to list, since
> the result is well-defined.
> --Scott David Daniels
> Scott.Daniels at Acm.Org
More information about the Python-list