Python Basics (collection)

Source: Internet
Author: User

The collection set in Python is a type of basic data that has both mutable sets (set ()) and immutable sets (Frozenset). Creating Collection Sets, collection set additions , collection deletions , intersections , Union sets, and difference sets is a very practical approach, followed by an introduction to the associated knowledge.

First, set characteristics

1, similar dictionary dict, but only key does not have value;

2, the storage of the collection is not in a fixed order

3, because the elements in the collection can not be repeated, so the collection is generally used to remove weight

Ii. definition of a set

There are two common ways to

1, directly define similar dictionaries but no value values, such as set1={"Openatck", "and", "List", "Dict", "Set"}2, create a set, provide a list as an input set Set2=set (["Openatck" , "and", "List", "Dict", "set"]) Two output result {' Openatck ', ' list ', ' and ', ' Set ', ' Dict '} {' openatck ', ' list ', ' and ', ' Set ', ' Dict ' }

Three, the operation of the collection element

1. Add elements can add elements using the Add () method

#!/usr/bin/env python#_*_coding:utf-8_*_set1={"Openatck", "and", "List", "Dict", "Set"}set2=set (["Openatck", "and", " List "," Dict "," set "]) Set2.add (" Winnerlook ") print (Set1,set2) Result: {' Set ', ' Openatck ', ' and ', ' dict ', ' list '} {' Openatck ' , ' Winnerlook ', ' list ', ' and ', ' dict ', ' Set '}

2. Add update () method for collection elements

Update () splits the element to be passed in as an individual into the collection

#!/usr/bin/env python#_*_coding:utf-8_*_set1={"Openatck", "and", "List", "Dict", "Set"}set2=set (["Openatck", "and", " List "," Dict "," Set "," Winnerlook "]) set2.update (" Winnerlook "," Mysqldba ") print (Set2) Result: {' B ', ' M ', ' r ', ' S ', ' Y ', ' e ', ' Winnerlook ', ' Set ', ' I ', ' Q ', ' and ', ' O ', ' w ', ' n ', ' dict ', ' K ', t} at this point we find that the values we have updated are all inserted into the current collection by each character as an element.

3. Deletion of the collection

In the previous list of learning we learned the clear (), pop (), remove (), Del and other methods can delete the list of elements, and then we analyze the collection of the method of deleting elements.


Clear () method

The clear () method, or just as in the list, empties all the elements in the collection, for example:

#!/usr/bin/env python#_*_coding:utf-8_*_set1={"Openatck", "and", "List", "Dict", "Set"}set2=set (["Openatck", "and", " List "," Dict "," Set "," Winnerlook "]) #set2. Update (" Winnerlook "," Mysqldba ") Set2.clear () print (Set1,set2) Result: {' Set ', ' Dict ', ' openatck ', ' list ', ' and '} set () Returns the result and is an empty collection, and the list uses the clear () method to return an empty list

Remove () method

The Remove () method removes an element from the collection, but the element must exist, or an error will be

#!/usr/bin/env python#_*_coding:utf-8_*_list=["Openatck", "and", "List", "Dict", "Set"]set1={"Openatck", "and", "list "," Dict "," Set "}set2=set ([" Openatck "," and "," List "," Dict "," Set "," Winnerlook "]) #set2. Update (" Winnerlook "," Mysqldba ") set2.remove (" Winnerlook ") List.remove (" and ") print (List,set2) Result: [' openatck ', ' list ', ' dict ', ' set '] {' Openatck ', ' and ', ' dict ', ' list ', ' Set '}

Pop () method

The Pop () method of the

  collection and the Pop () method of the list are not the same, the list can be removed with the underlying, and the list is the default to delete the last element, because the collection of storage is not in a fixed order, so the deleted element is random. Let's take a look at the following example, where the same program executes multiple times with inconsistent results.

#!/usr/bin/env python#_*_coding:utf-8_*_list=["Openatck", "and", "List", "Dict", "Set"]set1={"Openatck", "and", "list "," Dict "," Set "}set2=set ([" Openatck "," and "," List "," Dict "," Set "," Winnerlook "]) #set2. Update (" Winnerlook "," Mysqldba ") Set2.pop () List.pop () print (List,set2) results 1:[' openatck ', ' and ', ' list ', ' dict '] {' dict ', ' winnerlook ', ' and ', ' Set ', ' list '} #删除openstack结果2: [' openatck ', ' and ', ' list ', ' dict '] {' list ', ' Set ', ' and ', ' openatck ', ' Dict '} # Delete winnerlook results 3:[' openatck ', ' and ', ' list ', ' dict '] {' list ', ' Winnerlook ', ' openatck ', ' Set ', ' and '} #删除dict结果4: [' Openatck ', ' and ', ' list ', ' dict '] {' openatck ', ' dict ', ' winnerlook ', ' and ', ' list '} #删除set这里为什么是随机删除的, Personal guess is because the collection of stored order is not a fixed reason, so there will be random deletion phenomenon.

Del Delete

When you delete a defined collection, we can also take the Del method, so that a collection can be deleted, but this deletion will report that there is no error defining the collection.

#!/usr/bin/env python#_*_coding:utf-8_*_list=["Openatck", "and", "List", "Dict", "Set"]set1={"Openatck", "and", "list "," Dict "," Set "}set2=set ([" Openatck "," and "," List "," Dict "," Set "," Winnerlook "]) #set2. Update (" Winnerlook "," Mysqldba ") del Set2list.pop () print (List,set2) Result: Print (List,set2) nameerror:name ' Set2 ' is not defined

4. Access to the collection

Because the collection itself is unordered, you cannot create an index or slice operation for the collection, but you can iterate through or use in and not to access or judge the collection element.

5. The relationship between elements and sets, sets and sets

In our high school to learn the collection of knowledge, we learn the set of communication, aggregation, complement and other concepts, and this part of the concept in its Python programming data type collection is still applicable.

650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/M00/8F/37/wKiom1jX2uLAR_lvAAHJEC75CFw968.png-wh_500x0-wm_ 3-wmp_4-s_278262170.png "title=" Jihe.png "alt=" Wkiom1jx2ular_lvaahjec75cfw968.png-wh_50 "/>

Here is a simple demonstration of the following collection relationships and related actions:

The relationship of elements to a collection:

In is typically used to indicate whether an element is inside a collection, and the return value is ture and False

#!/usr/bin/env python#_*_coding:utf-8_*_list=["Openatck", "and", "List", "Dict", "Set"]set1={"Openatck", "and", "list "," Dict "," Set "}set2=set ([" OpenStack "," and "," List "," Dict "," Set "," Winnerlook "]) print (" OpenStack "in Set2) print (" Docker "in Set2) results: TrueFalse because Set2 contains openstack elements but does not contain Docker elements, a return ture is determined, one returns false

Not in actually the same meaning as in

#!/usr/bin/env python#_*_coding:utf-8_*_list=["Openatck", "and", "List", "Dict", "Set"]set1={"Openatck", "and", "list "," Dict "," Set "}set2=set ([" OpenStack "," and "," List "," Dict "," Set "," Winnerlook "]) print (" OpenStack "not in Set2) print ( "Docker" not in Set2) results: falsetrue

= = equals relationship

#!/usr/bin/env Python#_*_coding:utf-8_*_set2=set (["OpenStack", "and", "List", "Dict", "Set", "Winnerlook"]) set3={" OpenStack "," and "," List "," Dict "," Set "," Winnerlook "}print (set2==set3) Result: True means two sets equal set1={" OpenStack ", 9+8," list " , "Dict", "Set", "Winnerlook"}set2=set (["OpenStack", "and", "List", "Dict", "Set", "Winnerlook"]) set3={"OpenStack", 17, "List", "Dict", "Set", "Winnerlook"}print (set1==set3) Result: True indicates that two sets of equality expressions are the same as the result

! = Not equal to a relationship represents a different element of two collection

#!/usr/bin/env python#_*_coding:utf-8_*_set1={"OpenStack", 9+8, "list", "Dict", "Set", "Winnerlook"}set2=set ([" OpenStack "," and "," List "," Dict "," Set "," Winnerlook "]) set3={" OpenStack "," List "," Dict "," Set "," Winnerlook "}print (Set1!=set2) print (SET1!=SET3) results: TrueFalse

The relationship of collections to collections

Subset (<) and true subset (<=)

A subset represents an element within a collection that is contained by another collection, while a true subset is said to be equal to another set of elements.

#!/usr/bin/env python#_*_coding:utf-8_*_set1={"OpenStack", 9+8, "list", "Dict", "Set", "Winnerlook"}set2=set ([" OpenStack "," and "," List "," Dict "," Set "," Winnerlook "]) set3={" OpenStack "," List "," Dict "," Set "," Winnerlook "}set4= {"OpenStack", "List", "Dict", "Set", "Winnerlook"}print (set1<=set3) print (set4< set1) print (SET4<=SET3) Results: Truetruetrue

Strict superset (>) and non-strict superset (>=)

The relationship here is the same as the writing above, but it's just a matter of expressing the opposite relationship.

#!/usr/bin/env python#_*_coding:utf-8_*_set1={"OpenStack", 9+8, "list", "Dict", "Set", "Winnerlook"}set2=set ([" OpenStack "," and "," List "," Dict "," Set "," Winnerlook "]) set3={" OpenStack "," List "," Dict "," Set "," Winnerlook "}set4= {"OpenStack", "List", "Dict", "Set", "Winnerlook"}print (set1>=set3) print (SET4>SET1) print (SET4>=SET3) Results: Truefalsefalse

Intersection (&) or with intersection ()

Represents the same element in a two collection

#!/usr/bin/env python#_*_coding:utf-8_*_set1={"OpenStack", 9+8, "list", "Dict", "Set", "Winnerlook"}set2=set ([" OpenStack "," List "," Dict "," Winnerlook ", 99,100,0.99]) #set3 ={" OpenStack "," List "," Dict "," Set "," Winnerlook "}# set4={"OpenStack", "List", "Dict", "Set", "Winnerlook"}print (set1&set2) print (Set1.intersection (set2)) Result: {' OpenStack ', ' list ', ' dict ', ' Winnerlook '} {' OpenStack ', ' list ', ' dict ', ' Winnerlook '}

The Set (|) or in Union ()

Represents a total of two elements contained in a collection

#!/usr/bin/env python#_*_coding:utf-8_*_set1={"OpenStack", 9+8, "list", "Dict", "Set", "Winnerlook"}set2=set ([" OpenStack "," List "," Dict "," Winnerlook ", 99,100,0.99]) #set3 ={" OpenStack "," List "," Dict "," Set "," Winnerlook "}# set4={"OpenStack", "List", "Dict", "Set", "Winnerlook"} #print (Set1&set2) #print (Set1.intersection (Set2)) print ( Set1|set2) Print (Set1.union (Set2)) results: {0.99, Max, +, ' dict ', ' set ', +, ' winnerlook ', ' list ', ' OpenStack '}{0.99, 99, 100, ' Dict ', ' Set ', ' Winnerlook ', ' list ', ' OpenStack '}

Differential (-) or difference ()

Relative complement set

#!/usr/bin/env python#_*_coding:utf-8_*_set1={"OpenStack", 9+8, "list", "Dict", "Set", "Winnerlook"}set2=set ([" OpenStack "," List "," Dict "," Winnerlook ", 99,100,0.99]) #set3 ={" OpenStack "," List "," Dict "," Set "," Winnerlook "}# set4={"OpenStack", "List", "Dict", "Set", "Winnerlook"} #print (Set1&set2) #print (Set1.intersection (Set2)) print ( Set1-set2) Print (Set1.difference (Set2)) results: {, ' Set '}{17, ' set '}set2 relative sst1 of the complement is Set2, but not in Set1

Symmetric differential (^) or symmetric_difference ()

The symmetric difference is the XOR (' XOR ') of the set, and the obtained elements belong to the Set1,set2 but do not belong to both Set1 and Set2

#!/usr/bin/env python#_*_coding:utf-8_*_set1={"OpenStack", 9+8, "list", "Dict", "Set", "Winnerlook"}set2=set ([" OpenStack "," List "," Dict "," Winnerlook ", 99,100,0.99]) #set3 ={" OpenStack "," List "," Dict "," Set "," Winnerlook "}# set4={"OpenStack", "List", "Dict", "Set", "Winnerlook"} #print (Set1&set2) #print (Set1.intersection (Set2)) print ( Set1^set2) Print (Set1.symmetric_difference (Set2)) results: {0.99, +, +, +, ' set '}{0.99, +, (+), ' Set '}

6. And and or relationships between sets

Note that the and relationship is a value set2 or relationship is a value Set1

#!/usr/bin/env python#_*_coding:utf-8_*_set1={"OpenStack", 9+8, "list", "Dict", "Set", "Winnerlook"}set2=set ([" OpenStack "," List "," Dict "," Winnerlook ", 99,100,0.99]) #set3 ={" OpenStack "," List "," Dict "," Set "," Winnerlook "}# set4={"OpenStack", "List", "Dict", "Set", "Winnerlook"} #print (Set1&set2) #print (Set1.intersection (Set2)) print ( Set1 and Set2) print (Set1 or Set2) {0.99, ©, +, ' dict ', ' winnerlook ', ' list ', ' OpenStack '} {' dict ', ' Set ', ' Winnerlook ', 1 7, ' list ', ' OpenStack '}

7. Conversions between collections, lists, tuples, and strings

We can convert the collection to other formats based on different data types.

List (Set1) converts a collection into a list

Tuple (Set1) converts a collection to a Narimoto group

STR (SET1) converts a collection into a string

#!/usr/bin/env python#_*_coding:utf-8_*_set1={"OpenStack", 9+8, "list", "Dict", "Set", "Winnerlook"}set2=set ([" OpenStack "," List "," Dict "," Winnerlook ", 99,100,0.99]) #set3 ={" OpenStack "," List "," Dict "," Set "," Winnerlook "}# set4={"OpenStack", "List", "Dict", "Set", "Winnerlook"} #print (Set1&set2) #print (Set1.intersection (Set2)) print ( STR (SET1), type (str (SET1))) Print (list (SET1), type (list (SET1))) print (tuple (SET1), type (tuple (SET1))) Result: {' Set ', ' List ', ' Winnerlook ', ' dict ', +, ' OpenStack '} <class ' str ' >[' set ', ' list ', ' Winnerlook ', ' dict ', +, ' OpenStack '] &l T;class ' list ' > (' Set ', ' list ', ' Winnerlook ', ' dict ', ', ' OpenStack ') <class ' tuple ' >

8. Simple application of Set

Because the elements of the collection cannot be duplicated, they can be used to remove heavy

List2=[2,-3,-8,0,100,5005,737737,10010,-3,-8,0,100,10010]set222=set (list2) print (set222) results: {0, 2, 100, 737737, 5005 ,-8, 10010,-3}

The built-in function and the built-in method of set

1, Len (): Returns the number of collection elements

2. Set (), Frozenset () factory functions

3. All collection methods:

650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M02/8F/39/wKiom1jX67SQM9Y8AAHncmPnLjo334.png-wh_500x0-wm_ 3-wmp_4-s_2366317023.png "title=" Jihe2.png "alt=" Wkiom1jx67sqm9y8aahncmpnljo334.png-wh_50 "/>

4, only for the variable set of

650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/M02/8F/36/wKioL1jX6_bSV6iOAAHBQYpAyW4283.png-wh_500x0-wm_ 3-wmp_4-s_4150361123.png "title=" Jieh3.png "alt=" Wkiol1jx6_bsv6ioaahbqypayw4283.png-wh_50 "/>

650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M00/8F/36/wKioL1jX6_ezBeapAAF4tjRwMjY858.png-wh_500x0-wm_ 3-wmp_4-s_1527912733.png "title=" Jihe4.png "alt=" Wkiol1jx6_ezbeapaaf4tjrwmjy858.png-wh_50 "/>

Reference Document: Http://www.cnblogs.com/BeginMan/p/3160565.html

This article is from the "Keep Dreaming" blog, please be sure to keep this source http://dreamlinux.blog.51cto.com/9079323/1910566

Python Basics (collection)

Related Article

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.