Features of Set
the internal structure of set is much like the dict, the only difference is that it does not store value, so it is very fast to determine whether an element is in set.
The set stored element is similar to the Dict key, and must be an immutable object , so any mutable object cannot be placed in the set.
Finally, the set stores the elements that are not in order.
Where can these features of set be applied?
Monday to Sunday you can use the string ' MON ', ' TUE ', ... ' SUN ' said.
Suppose we let the user enter a day from Monday to Sunday, how to tell if the user's input is a valid week?
Can be judged with an if statement , but this is tedious:
x = ' Mon ' # user Input string if x = = ' MON ' or x = = ' TUE ' or x = = ' WED ' or x = = ' THU ' or x = = ' FRI ' or x = = ' SAT ' or x! = ' SUN ': Print (' input ok ') Else: print (' input error ')
If you create good one set beforehand, include ' MON ' ~ ' SUN ':
Weekdays = set ([' Mon ', ' TUE ', ' WED ', ' THU ', ' FRI ', ' SAT ', ' SUN ']) x = ' Mon ' # user Input string if x in weekdays: print (' Input O K ') Else: print (' input error ')
In this way, the code is much simpler.
Python Set Features