Quick question, I was coding a small program in Python and was looking for a way to make a condition to only allow the input of 4 numbers if that input has the same len that I want and to make sure that all the numbers in it are unique.
After searching for a bit on the web I found several people giving the same solution, it was len(set(input)) == size
(size of my number) in my case.
It works well, but I fail to understand how it really works and I want to know.
I've also tried removing the len
from that and it worked anyway so I have little clue what are the effects of each thing in this small piece of code.
So even though it allows me to create a condition that makes sure each of the 4 numbers are unique I would love it if someone could explain to me how it works.
Thanks in advance.
PS: If anyone wonders what it is I'm practicing by making my own version of Cows & Bulls. So given a random number using random.sample the user then has to try to guess what the number is. The conditions I mentioned were used upon the user's input.
A set
is a datastructure for:
constructing and manipulating unordered collections of unique elements.
These collections have to be hashable as well. But we are lucky: strings and characters are hashable.
The point is that if we construct a set form set(['1','4','1','5','2'])
, it will thus construct a collection of unique elements. So adding '1'
twice here, does not makes any difference, the result is set(['1', '4', '5', '2'])
(or more conveniently written {'1', '4', '5', '2'}
). With len(..)
we obtain the size of a collection. So the size of that set is the number of unique characters of the input.
So if we write len(set(some_string))
we will first turn a string into a set. This is possible since a string is an iterable of the characters. So Python sees a string as an ordered collection of characters. 'abc'
, is a collection of 'a'
, 'b'
, and 'c'
. So every digit is seen as an element and added to the set.
But adding the same digit a second time, has no effect. So that means that we end up with a set where every digit is added once. By then calculating the len(..)
we obtain the number of unique digits. We can then compare that number with a given number.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With