here is my code
I define an empty set, I want to compare
se([i]) > 0, then reassign the value of se([i]) to be 0. I am not able to do this
since se([i]) is a set and cannot be compared to int. kindly help. I am new to python programming.
se =set()
se.update([8])
print (se)
for i in range (10):
se.update([i])
print type(se)
print len(se)
print se
If you’re just trying to compare each element of
seto0, and replace the ones that are> 0with0, that’s pretty easy:Or, if you think about it:
You can’t do this by indexing
se[i], because sets aren’t indexable, because the whole point of sets (both mathematical and Python) is that they’re unordered. And you definitely can’t do it by callingse([i]), because yousets aren’t functions, or other callable (function-like) things. If you really wanted to do it by mutating in place, you could:(Notice the
se.copy()there—you can’t change the shape of a collection while iterating over it, so you need to iterate over a copy of it instead.)Meanwhile, again, the whole point of a set is that it’s unordered, which means adding
0multiple times is exactly the same as adding it once. So:Or, using your code (with some of the extra
printstatements removed for brevity):