Python101: 15. Set

post thumb
Python
by Admin/ on 13 Jul 2021

Python101: 15. Set


Python also contains a set type. A set is an unordered set of non-repeating elements. Its basic usage includes member detection and elimination of duplicate elements. Set objects also support mathematical operations like unions, intersections, difference sets, symmetric differences, and so on.

The set structure is as follows:


set1 = {'hello', 'hello', 'word', 'word'}
set1
# The output is automatically de-duplicated
# {'hello', 'word'

1. Collection creation


Collections can be created using curly braces { } or set() functions.

Create format:

parame = {value01, value02, ...}
or
set(value)

Note: You must use set() instead of { } to create an empty set, because { } is used to create an empty dictionary.

# Create an empty set
empty_set = set()
type(empty_set)
# <class 'set'>

# Create an empty dictionary
empty_dict = {}
type(empty_dict)
# <class 'dict'>

2. Basic operations of the collection


2.1 Adding elements

Syntax format:

s.add(x)

Adds an element x to the set s. If the element already exists, then no operation is performed.

s = set(('hello','world'))
print(s)

# Add an element to the set s
s.add('!')
print('The set after adding elements is: %s' % s)

# The output is.
# The set after adding the elements is: {'world', '!' , 'hello'}

In addition to the add() method, which adds elements, there is a method that also adds elements, and the arguments can be lists, tuples, dictionaries, etc. The syntax format is as follows.

s.update( x )

The parameter x can be one or more than one, with a comma separating the multiple parameters

# 1) Add a list
s.update([1,3],[2,4])
print('The set after adding elements is: %s' % s)

# 2) Add a tuple

s.update(('h', 'j'))
print('The set after adding elements is: %s' % s)

2.2 Removing elements

The ** syntax format is as follows:**

s.remove( x )

Remove the element x from the set s. If the element does not exist, an error will occur.

# Remove element 2 from the set
s.remove(2)
print('The set after removing element 2 is: %s' % s)

# Exceptions are thrown if you remove an element that does not exist in the collection
# Remove a collection that does not exist in the set
s.remove('hi')
print('The set after removing elements is: %s' % s)

# Exception messages
# Traceback (most recent call last):
#   File "test.py", line 20, in <module>
#   s.remove('hi')
#   KeyError: 'hi'

There is also a method to remove an element from a collection and no error occurs if the element does not exist. The format is shown below.

s.discard( x )
thisset = set(("Google", "Runoob", "Taobao"))
thisset.discard("Facebook") # No error will occur if it doesn't exist
print(thisset)
# {'Taobao', 'Google', 'Runoob'}

We can also set up the random deletion of an element in a collection with the following syntax format.

s.pop()
# Randomly delete an element of the set
print(s)

s.pop()

print('The set after removing elements is: %s' % s)

# Output results.
# {1, 3, 4, 'world', '!' , 'hello', 'h', 'j'}
# The set after removing the elements is: {3, 4, 'world', '! , 'hello', 'h', 'j'}

Note: In interactive mode, pop is to delete the first element of the set (the first element of the sorted set).

2.3 Calculating the number of elements of a set

The syntax format is as follows:

len(s)

Computes the number of elements of the set s.

print('The length of the set s is: %s' % len(s))

# Output results
The length of the set s is: 7

2.4 Emptying a collection

The syntax format is as follows:

s.clear()

Empty the set s

s.clear()
print('The result after the collection is cleared is: %s' % s)

# Output results.
# The result after the set is cleared is: set()

2.5 Determining whether an element exists

The syntax format is as follows:

x in s

Determine if element x is in the set s. Returns True if it exists, and False if it does not.

# Determine if the element exists
s = {'hello', 'word'}
# Determine if the element hello is in the set s

print(hello' in s)

# Output result: True

2.6 Set operations

The operators between sets are ‘-’, ‘|’, ‘&', ‘^'; the following is an example of the operation between two sets The following is an example of an operation between two sets.

  • -': represents that the former contains elements not contained in the latter
  • |': represents the result of the result of the de-weighting of all the elements in the two sets together
  • &': the elements contained in both
  • ^': elements that are not contained in both sets
a = set('afqwbracadaagfgbrafg')
b = set('rfgfgfalacazamddg')

a
# {'r', 'q', 'd', 'b', 'w', 'g', 'f', 'c', 'a'}

b
# {'r', 'd', 'g', 'f', 'l', 'z', 'c', 'm', 'a'}

# The elements contained in set a but not in set b
a - b
# {'b', 'w', 'q'}

 # All elements contained in set a or b
a | b
# {'d', 'g', 'l', 'c', 'r', 'q', 'b', 'w', 'f', 'z', 'm', 'a'}

# The elements contained in both sets a and b
a & b
# {'r', 'd', 'g', 'f', 'c', 'a'}

# Elements not contained in both a and b
a ^ b
# {'l', 'q', 'b', 'w', 'z', 'm'}

3. Set Derivative


Like lists, collections support derivatives

# Determine if an element exists
a = {x for x in 'abracadabra' if x not in 'abc'}
a
# {'r', 'd'}

4. Collection built-in methods


4.1 difference()

The difference() method is used to return the difference set of a collection, i.e., the elements of the returned collection are contained in the first collection but not in the second collection (the method’s argument), returning a new collection. difference()` method syntax:

set.difference(set)

Example:

The difference between two sets returns a set whose elements are contained in set x, but not in set y.

# Find the difference between two sets whose elements are in x but not in y
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}

z = x.difference(y)

print('The difference set of the two sets is: %s' % z)

# The output is.
# {'cherry', 'banana'}

4.2 difference_update()

  • The difference_update() method is used to remove elements that exist in both collections.
  • The difference_update() method differs from the difference() method in that the difference() method returns a new collection with the same elements removed, while the difference_update() method removes elements directly from the original collection with no return value.
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}

x.difference_update(y)

print(x)
# The result is.
# {'banana', 'cherry'}


x1 = {1,2,3,4}
y1 = {1,2,3}

x1.difference_update(y1)

print(x1)

# The result is.
# {4}

4.3 intersection()

The intersection() method is used to return the elements contained in two or more collections, i.e., the intersection, returning a new collection.

intersection() method syntax:

set.intersection(set1, set2 ... etc)

# **Parameters:**
# set1 -- required, the set to find the same elements
# set2 -- optional, other sets to find the same elements, can be more than one, more than one use comma , separated by

Example:

# Returns the intersection of two or more sets
x = {"apple", "banana", "cherry"}
y = {"google", "runoob", "apple"}

z = x.intersection(y)

print(z)

# Returns the intersection of three sets
x = {"a", "b", "c"}
y = {"c", "d", "e"}
z = {"f", "g", "c"}

result = x.intersection(y, z)
print('The difference set of the three sets is: %s' % result)

# Output results.

# {'apple'}
# The difference set of the two sets is: {'c'}

4.4 intersection_update()

  • The intersection_update() method is used to get the elements of two or more collections that overlap, i.e. to compute the intersection.
  • The intersection_update() method differs from the intersection() method in that the intersection() method returns a new set, while the intersection_update() method removes the non-overlapping elements from the original set.

intersection_update() method syntax:

set.intersection_update(set1, set2 ... etc)

# **Parameters**
# set1 -- required, the set to find the same elements
# set2 -- optional, other sets to find the same elements, multiple sets can be used, multiple sets are separated by a comma ','

Example:

# Returns the intersection of a set with no return value
x = {"apple", "banana", "cherry"}
y = {"google", "runoob", "apple"}

x.intersection_update(y)

print(x)

x = {"a", "b", "c"}
y = {"c", "d", "e"}
z = {"f", "g", "c"}

x.intersection_update(y, z)

print(x)

# Output results.
# {'apple'}
# {'c'}

4.5 union()

The union() method returns the union of two collections, i.e. all the elements of the collection are included, the duplicate elements will only appear once, and the return value returns a new collection

Syntax:

# Syntax of the `union()` method.

set.union(set1, set2...)
# Parameters
# set1 -- required, the target set to be merged
# set2 -- optional, other sets to be merged, can be multiple, multiple separated by commas.

Examples:

# Merge two sets where duplicate elements will only appear once.

x = {"apple", "banana", "cherry"}
y = {"google", "runoob", "apple"}

z = x.union(y)

print(z)
# The output is.
# {'cherry', 'runoob', 'google', 'banana', 'apple'}


# Merge multiple collections.

# Example 1
x = {"a", "b", "c"}
y = {"f", "d", "a"}
z = {"c", "d", "e"}

result = x.union(y, z)

print(result)
# The output results are.
# {'c', 'd', 'f', 'e', 'b', 'a'}

4.6 isdisjoint()

The isdisjoint() method is used to determine if two collections contain the same elements, == returns True if they do not, False otherwise. == The isdisjoint() method is used to determine if two collections contain the same elements, == returns True if they do not, otherwise returns False.

Syntax:

# isdisjoint() method syntax.

set.isdisjoint(set)

Example:

x = {"apple", "banana", "cherry"}
y = {"google", "runoob", "apple"}
# Determine if the set y contains elements of the set x. If not, return True, if yes, return False
z = x.isdisjoint(y)
# The result returns False, that the set y has the same elements as x
print(z)

x = {"apple", "banana", "cherry"}
y = {"google", "runoob", "baidu"}
# Determine if the set y contains elements of the set x. If not, return True, if yes, return False
z = x.isdisjoint(y)
# The result returns True, that is, the set y does not have the same elements as x
print(z)

# Output results.
# False
# True

4.7 issubset()

The issubset() method is used to determine if all elements of a set are contained in the specified set, and returns True if they are, otherwise returns False.

Syntax:

issubset() method syntax.

set.issubset(set)

# **parameters**
# set -- required, to be more than the found set
# Return Value
# Returns a boolean value, True if both are included, False otherwise.

Example:

# Determine if all elements of set x are contained in set y.
x = {"a", "b", "c"}
y = {"f", "e", "d", "c", "b", "a"}

z = x.issubset(y)

print(z)

# Output results
# Description The elements of the set x are contained in y
# True

Note: All elements in the set must be included, otherwise the result is False

# The set y contains only elements b and c, and the result is False
x = {"a", "b", "c"}
y = {"f", "e", "d", "c", "b", "y"}

z = x.issubset(y)

print(z)

# The output of the result.
# False

4.8 issuperset()

The issuperset() method is used to determine if all elements of the specified set are contained in the original set, and returns True if they are, otherwise returns False.

Syntax:

set.issuperset(set)

Example:

# Determine if all elements of the set y are contained in the set x.
x = {"f", "e", "d", "c", "b", "a"}
y = {"a", "b", "c"}

z = x.issuperset(y)

print(z)
# The output is.

# True


# Return False if not all are included.

# Example 1
x = {"f", "e", "d", "c", "b"}
y = {"a", "b", "c"}

z = x.issuperset(y)

print(z)
# The output is.

# False

4.9 symmetric_difference()

The symmetric_difference() method returns the set of non-duplicate elements in the two collections, i.e., it removes the elements that exist in both collections and returns a new collection as a result.

Syntax:

set.symmetric_difference(set)

Example:

# Returns a new set of two collections, but removes the duplicate elements of the two collections.
x = {"apple", "banana", "cherry"}
y = {"google", "runoob", "apple"}

z = x.symmetric_difference(y)

print(z)

# Output results.
# {'banana', 'google', 'cherry', 'runoob'}

4.10 symmetric_difference_update()

The symmetric_difference_update() method removes the elements of the current collection that are identical in another specified collection and inserts the elements of another specified collection that are different into the current collection.

Syntax:

set.symmetric_difference_update(set)

Example:

# Remove the duplicate elements in the original set x from the set y and insert the non-duplicate elements into the set x.
x = {"apple", "banana", "cherry"}
y = {"google", "runoob", "apple"}

x.symmetric_difference_update(y)

print(x)

# Output results.
# {'runoob', 'cherry', 'banana', 'google'}

Postscript: Several other methods are to add, delete, and check the collection, such as: add() clear() copy() update() pop() remove() discard() and other methods, these methods are detailed in the basic operation of the collection chapter, we will use as needed.

Summary


This section introduces you to the manipulation and use of collections in Python data structures, and provides some basic knowledge and practical support for Python engineers working with collections.


Reference.

http://www.pythondoc.com/pythontutorial3 https://www.runoob.com/python3/python3-set.html https://github.com/JustDoPython/python-100-day/tree/master/day-015

comments powered by Disqus