[Tutor] Sets question
Mats Wichmann
mats at wichmann.us
Wed Apr 26 20:56:40 EDT 2017
On 04/26/2017 06:33 PM, 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?
>
A little confused... why not just create it the way you want it? How do
you end up with {'1'} ?
Since sets are unordered, you can't use indexing to change a value, but
you can discard a value and add a new value in the form you want.
but Python isn't terribly friendly to identifying if something is a
string... you could do something grotty like this I suppose:
for f in some_set:
if isinstance(f, str):
some_set.discard(f)
some_set.add(int(f))
(top of head, not really experimented with)
More information about the Tutor
mailing list