Python Set Features

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.