# SET operations in Python

Source: Internet
Author: User
Tags iterable

Similar to other languages, python set is a disordered, non-repeating element set. Its basic functions include link testing and deduplication. the Set object also supports mathematical operations such as union, intersection, difference, and sysmmetric difference.

Sets support x in set, len (set), and for x in set. As an unordered set, sets do not record element positions or insertion points. Therefore, sets do not support indexing, slicing, or sequence-like operations.

>>> Basket = ['apple', 'Orange ', 'apple', 'pear', 'Orange ', 'Banana']
>>> Fruit = set (basket) # create a set without duplicates
>>> Fruit
Set (['Orange ', 'pear', 'apple', 'bana'])
>>> 'Orange 'in fruit # fast membership testing
True
>>> 'Crabgrass 'in fruit
False
>>># Demonstrate set operations on unique letters from two words
...
>>> A = set ('abracadaba ')
>>> B = set ('acazam ')
>>> A # unique letters in
Set (['A', 'R', 'B', 'C', 'D'])
>>> A-B # letters in a but not in B
Set (['R', 'D', 'B'])
>>> A | B # letters in either a or B
Set (['A', 'C', 'R', 'D', 'B', 'M', 'z', 'L'])
>>> A & B # letters in both a and B
Set (['A', 'C'])
>>> A ^ B # letters in a or B but not both
Set (['R', 'D', 'B', 'M', 'z', 'L'])

Len (s)

Set length

X in s

Test whether x is a member of s.

X not in s

Test whether x is not a member of s

S. issubset (t)
S <= t
Test whether every element in s is in t

S. issuperset (t)
S> = t
Test whether every element in t is in s

S. union (t)
S | t
Returns a new set containing every element of s and t.

S. intersection (t)
S & t
Returns a new set containing the public elements in s and t.

S. difference (t)
S-t
Returns a new set that contains elements in s but not in t.

S. symmetric_difference (t)
S ^ t
Returns a new set that contains no repeated elements in s and t.

S. copy ()

Returns a shortest copy of set "s ".

Note: non-operator of union (), intersection (), difference (), and equalric_difference () is like s. the union () version will accept any iterable as a parameter. Conversely, operator-based counterparts requires that the parameters must be sets. This avoids potential errors, such as using set ('abc') & 'cbs 'to replace set ('abc'). intersection ('cbs') for better readability '). Changes made from version 2.3.1: all previous parameters must be sets.

In addition, both Set and ImmutableSet support comparison between set and set. Two sets are equal only in this case: each set element is an element in the other (the two are subsets ). A set is smaller than another set. Only when the first set is the subset of the second set (a subset, but not equal ). A set is better than another set, only when the first set is the superset of the second set (it is a superset, but not equal ).

Sub-set and equal comparison do not produce a complete sorting function. For example, any two sets are not equal and are not sub-sets. Therefore, the following operations return False: a <B, a = B, or a> B. Therefore, the _ cmp _ method is not provided for sets.

Because sets only define part of the sorting function (subset relation), the output of list. sort () method is not defined for sets list.

Operator
Calculation Result

Hash (s)
Returns the hash value of s.

The following table lists the operations that enable Set availability for ImmutableSet unavailability:

Operator (voperator)
Equivalent
Calculation Result

S. update (t)
S | = t
Returns the set "s" after adding an element in set "t"

S. intersection_update (t)
S & = t
Only set "s" containing the elements in set "t" are returned"

S. difference_update (t)
S-= t
Returns the set "s" after the elements in set "t" are deleted"

S. effecric_difference_update (t)
S ^ = t
Returns the set "s" that contains the set "t" or set "s" element instead of both"

Add element x to set "s"

S. remove (x)

Deletes element x from set "s". If the element does not exist, KeyError is thrown.

If element x exists in set "s", delete

S. pop ()

Delete and return an uncertain element in set "s". If it is null, KeyError is thrown.

S. clear ()

Delete all elements in set "s"

Note: Non-operator version update (), intersection_update (), difference_update (), and effecric_difference_update () will accept any iterable as the parameter. Changes made from version 2.3.1: all previous parameters must be sets.

Note: This module also contains a union_update () method, which is an alias for the update () method. This method is included for backward compatibility. Programmers should use the update () method more, because this method is also supported by the built-in set () and frozenset () types.

Related Keywords:

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.