+ 1
How to get the only element of a set?
I write a program to solve sudoku. I store all the optional assinments for a specific cell, e.g. t = {2,4,7}. While processing the data, I remove 2 and 7 from t. Then the desired condition len(t) = 1 is True. How can I get the only element from t, viz. 4? I tried .pop method, but it changes t. What can I do? Maybe a "for i in t" would help?
6 odpowiedzi
+ 5
this question fascinates me, so I did some research and found that:
http://stackoverflow.com/questions/1619514/how-to-extract-the-member-from-single-member-set-in-JUMP_LINK__&&__python__&&__JUMP_LINK
does this help?
0
you can't just take it?
if len(t)==1:
cell=t[0]
0
No, a set is not indexed.
0
yup, sorry I missed set. thought you had a list.. a for loop would work
t={1}
for i in t:
break
print(i)
0
http://www.sololearn.com/app/JUMP_LINK__&&__python__&&__JUMP_LINK/playground/cxk4T69d8b6H/
here's a way for doing that.let me know if it's useful :)
0
Thanks all.
I used tuple unpacking:
a, = b
when b is the one-element set.