1

In the following code;

all_digits = set(range(10))
print all_digits
for i in range(102,167):
 digits = set(k for k in (str(i)))
 if len(digits) != 3:
 continue
 print "digits:", digits
 remaining_digits = all_digits - digits
 print "remaining:", remaining_digits

The digits set contains 3 elements. I want a set difference of them, however, remaining_digits always have all digits. What am I doing wrong here? Here is a sample from output I am getting;

set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
digits: set(['1', '0', '2'])
remaining: set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
digits: set(['1', '0', '3'])
remaining: set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
digits: set(['1', '0', '4'])
remaining: set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
digits: set(['1', '0', '5'])
remaining: set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
digits: set(['1', '0', '6'])
remaining: set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
digits: set(['1', '0', '7'])
remaining: set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
digits: set(['1', '0', '8'])
remaining: set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
asked Aug 27, 2012 at 22:44

1 Answer 1

7

all_digits is a set of the ten integers 0 through 9, whereas digits is a set of 1-character strings like "0". Since the types are different, the set difference doesn't work as expected.

To make it work like you want, either use all integers or use all strings, e.g.:

all_digits = set(map(str, range(10)))
answered Aug 27, 2012 at 22:47
Sign up to request clarification or add additional context in comments.

1 Comment

Ahh, such a rookie mistake. Sorry for taking your time.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.