[Tutor] Sets question
Peter Otten
__peter__ at web.de
Thu Apr 27 05:49:55 EDT 2017
Phil wrote:
> Another question I'm afraid.
>
> If I want to remove 1 from a set then this is the answer:
>
> set([1,2,3]) - set([1])
>
> I had this method working perfectly until I made a change to cure another
> bug.
>
> So, I have a set represented in the debugger as {1,2,3} and again I want
> to remove the one. Only this time the one set is represented as {'1'} and,
> of course {'1'} is not in the set {1,2,3}.
>
> Ideally, I would like {'1'} to become {1}. Try as I may, I have not
> discovered how to remove the '' marks. How do I achieve that?
If your code now looks like this
>>> s = {1, 2, 3}
>>> v = "1"
>>> s = s - set([int(v)])
>>> s
{2, 3}
or this
>>> s = {1, 2, 3}
>>> s = s - {int(v)}
>>> s
{2, 3}
I'd like to bring to your attention the discard() method
>>> s = {1, 2, 3}
>>> s.discard(int(v))
>>> s
{2, 3}
which allows you to avoid building the throwaway single-entry set.
More information about the Tutor
mailing list