Python tutorial Share 6- collection

6.1 Collection Creation and use

6.1.1 collection creation and deletion

Variable is set unordered sequence, which uses a pair of braces ({}) as a delimiter, separated by commas between the elements, unique, and can not be duplicated between different elements in each element are a set. In the collection, only contain numbers, strings, etc. tuples immutable types of data, not contain a list of variable types, dictionaries, and other data collection. Python provides a built-in function hash function to calculate a hash value () object, who can not calculate a hash value ( call the object hash () throws an exception when a function) can not exist as an element of the set, nor as a dictionary object the key to use.

In Python , the collection directly assigned to the variable to create a collection of objects. Or may use the set function to convert other iteration object list, as the set of tuples, if the original data elements exist in duplicate, then the set time is converted to a leaving only. Use of these two methods the following example:

>>> Kee = {3,5} # directly set assigned to the variable

>>> print(Kee)

{3,5}

>>> kee = set ([0,1,2,3,4,5,6,6,6,7,8,9] ) # using the set () functions, automatic conversion element deduplication

>>> print(kee)

{0,1,2,3,4,5,6,7,8,9}

 

When you no longer use a collection, you can use the del command to delete the entire set, then the above code, if you want to delete the entire collection Kee , you can directly enter the following command:

the del Kee

 

6.1.2 element insertions and deletions

Collection of objects using add () method can be set to increase as a new element, if the element already exists in the collection operation is ignored; using the update () method elements may be incorporated into another set of current collection. Use example:

>>> Kee = {1,2,3}

>>> Kee.add (3) # add elements 3 , because 3 is already Kee exist in the collection, so ignore this operation

>>> print(Kee)

{1,2,3}

>>> Kee.update ({3,4}) # a {3,4} is added to the elements of the collection Kee collection, and ignore duplicate elements 3

>>> print(Kee)

{1,2,3,4}

 

Collection of objects pop () method is used to remove and returns a random element in the set if the set is empty exception is thrown; Remove () method is used to delete elements in the set, if the specified element does not exist exception is thrown; disscard () function is used to delete a specific element from the collection, if the element is not set then ignore this operation does not throw an exception; the Clear () function deletes empty set of all elements in the collection. The method of this example instruction format of several elements in the set to delete the following:

>>> Kee = {1,2,3,4,5,6,7,8,9}

>>> Kee.pop () # use pop () function randomly delete Kee an element in the collection

>>> print(Kee)

{1,2,3,4,5,6,7,8}

>>> Kee.remove (2) # Use remove () function deletes the specified element 2

>>> print(Kee)

{1,3,4,5,6,7,8}

>>> Kee.disscard (1) # use disscard () function deletes the specified element 1

>>> print(Kee)

{3,4,5,6,7,8}

>>> Kee.clear () # using the clear () function Clear Kee all values in the set

>>> print(Kee)

{}

 

In addition, there display length and a test set of operations. Function displays the set length is len () , but it should be noted that, len () function when displaying the set length, does not show the number of repeating elements; if you want to test whether an element is a member of a set, it is necessary used in testing, and basic usage is:

l 'an element' in a collection

 

6.2 common set of operations

It includes a list of the kind set, set, multiple sets, and trees. List or enumeration type can be set. The best application is set to repeat, that it can be understood mathematical meaning, in Python language, specifically refers to a collection of the variable set (SET ()) and a set of immutable (frozenset) are two, and more commonly used It is set collection. set and other similar language, is an unordered collection of unique elements, basic functions include testing and eliminating duplicate elements. Collection object supports union ( joint ) , intersection ( cross ) , -difference ( difference ) and sysmmetric difference ( symmetric difference ) other mathematical operations. To set s and a collection of t as an example, methods for the collection are the following:

s.issubset L (t) : test whether s of each element in t ; and

s.issuperset L (t) : test whether t Each element in s ; and

s.union L (t) : Returns a new set comprising s and t each of the elements;

s.difference L (t) : Returns a new set comprising s have but t is not an element;

s.intersection L (t) : Returns a new set comprising s and t are the common elements;

s.symmetric_difference L (t) : Returns a new set comprising s and t are not repeated elements.

 

issubset () method is used to test whether s each element in the set in t the set. If t all elements are not in the s , the result returns False . Its application method is:

l s.issubset(t)

 

union () method returns a new set containing s and t all elements. Basic usage is:

l s.union(t)

 

intersection () method returns a new set containing s and t are the common elements. E.g. s included in the set {A , B , C , 0} , T is included in the {1,2,3,0} , the use of this method, which returns two sets of common elements {0} . Its basic usage is as follows:

l s.intersection(t)

 

Back command element does not overlap The symmetric_difference () method, which returns for a new set set, this set comprising s there but t no elements, or to s and t as an example, s , and t using the command after, it will return a new set set {a , B , C , l, 2,3}. its basic use is as follows:

l s.symmetric_difference(t)

 

If you want to return only s exist in the collection t element in the set does not exist, it is necessary to use the command difference () method, a return instruction is only for the new set comprises s have but t is not an element. If s set and t set to use, i.e., it returns the set {A , B , C} . It is used as follows:

l s.difference(t)

Guess you like

Origin www.cnblogs.com/yiyi314/p/11038379.html