Collection of Python

Source: Internet
Author: User

I. Introduction to the Collection

1. The collection is born to go heavy

2. The set is also unordered

3. The role of the set is mainly relationship testing, testing the intersection of two sets of data, difference set, set, subset, parent set, symmetric (inverse) difference set and other relationships

Second, the use of the collection method

A = {6,7,1,2,3,4,5} #创建一个集合

B=set ([1,2,3,4,8]) #创建一个集合

A & B #交集, which is present in a and B, running result: {1, 2, 3, 4}

A | B # Union, result of merge of A and B, operation result: {1, 2, 3, 4, 5, 6, 7, 8}

A-B #差集, A is not in a, run result: {5, 6, 7}

A ^ B # symmetric difference set, A and b not each other, running result: {5, 6, 7, 8}

A.add (' x ') #集合中添加一项

A.update ([888,999]) #集合中添加多项

A.remove (999) #删除一项, there will be an error

A.discard (999) #删除一项, there is no error

A.issubset (b) #a是否是b的子集

A.issuperset (b) #a是否是b的父集

Example (check if the password contains uppercase, lowercase letters, numbers, special characters)

All_nums = Set (String.digits)
Lower = set (String.ascii_lowercase)
Upper = set (String.ascii_uppercase)
Punctuation = Set (string.punctuation)
For I in range (5):
PWD = input (' Please enter your password: '). Strip ()
PWD = set (PWD)
If PWD & All_nums and PWD & Lower and PWD & Upper and PWD & punctuation:
Print (' password valid ')
Else
Print (' Password is not legal! ‘)

Collection of Python

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.