Summary of Python set (set) type operations
Python's set is similar to other languages. It is an unordered set of non-repeating elements. Its basic functions include relationship testing and elimination of duplicate elements. Set objects also support union, intersection, difference and sysmmetric difference( Symmetric difference set) and other mathematical operations.
sets supports x in set, len(set), and for x in set. As an unordered collection, sets do not record element positions or insertion points. Therefore, sets do not support indexing, slicing, or other sequence-like operations.
Let’s give a simple example below.
>>> x = set('spam')
>>> y = set(['h','a','m'])
>>> x, y
(set([ 'a', 'p', 's', 'm']), set(['a', 'h', 'm']))
Here are some small applications.
>>> x & y # Intersection
set(['a', 'm'])
>>> x | y # Union
set(['a', 'p', 's ', 'h', 'm'])
>>> x - y # Difference set
set(['p', 's'])
I remember a netizen asked how to remove duplicate elements from a massive list , it's okay to use hash to solve it, but it doesn't feel very high in terms of performance. It's still very good to use set to solve it. The example is as follows:
>>> a = [11,22,33,44,11,22]
>>> b = set(a)
>>> b
set([33, 11, 44, 22])
>>> c = [i for i in b]
>>> c
[33, 11, 44, 22]
It’s very cool and can be done in just a few lines.
1.8 Sets
Collections are used to contain a group of unordered objects. To create a set, use the set() function and provide a series of items like this:
s = set([3,5,9,10]) #Create a numeric set
t = set("Hello" ) #Create a collection of unique characters
Unlike lists and tuples, collections are unordered and cannot be indexed numerically. Additionally, elements in the collection cannot be repeated. For example, if you check the value of the t set in the previous code, the result will be:
>>> t
set(['H', 'e', 'l', 'o'])
Note that only an 'l'.
Sets support a series of standard operations, including union, intersection, difference and symmetric difference, for example:
a = t | s # The union of t and s
b = t & s # The union of t and s Intersection
c = t – s # Find the difference set (the term is in t, but not in s)
d = t ^ s # Symmetrical difference set (the term is in t or s, but not both at the same time) Medium)
Basic operations:
t.add('x') . ) can delete one item:
t.remove('H')
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
Test whether every element in s is in t
s.issuperset(t)
s >= t
Tests whether every element in t is in s
s.union(t)
s | t
Returns a new set containing every element in s and t
s.intersection(t)
s & t
Returns a new set containing common elements in s and t
s.difference(t)
s - t
Returns a new set containing elements that are in s but not in t
s.symmetric_difference(t)
s ^ t
Returns a new set containing unique elements in s and t
s.copy()
Returns a shallow copy of set "s"
Please note : The non-operator (non-operator (that is, of the form s.union()) versions of union(), intersection(), difference() and symmetric_difference() will accept any iterable as argument. In contrast, their operator based counterparts require that the arguments must be sets. This avoids potential errors such as using set('abc') & 'cbs' instead of set('abc').intersection('cbs') for more readability. Changes from version 2.3.1: Previously all parameters had to be sets.
In addition, both Set and ImmutableSet support comparison between sets. Two sets are equal if and only if: the elements of each set are elements of the other (they are each other's subset). One set is smaller than another set only if the first set is a subset of the second set (a subset, but not equal). One set is stronger than another set only if the first set is a superset of the second set (a superset, but not equal).
Subsets and equality comparisons do not produce full sorting functionality. For example: any two sets are neither equal nor subsets of each other, so the following operations will return False: a
b. Therefore, sets does not provide a __cmp__ method. Because sets only defines part of the sorting function (subset relationship), the output of the list.sort() method is not defined for the list of sets.Operator
Operation result
hash(s)
Returns the hash value of s
The following table lists the operations available for Set but not available for ImmutableSet:
operator (voperator)
is equivalent to
operation result
s.update(t)
s |= t
Returns set "s"
s.intersection_update(t)
s &= t
after adding elements in set "t". Returns only set "s"
s containing elements in set "t". difference_update(t)
s -= t
Returns set "s" after deleting the elements contained in set "t"
s.symmetric_difference_update(t)
s ^= t
Returns set "t" ” or set “s” that has elements in set “s” but not both
s.add(x)
adds element x to set “s”
s.remove(x)
Removes the element Returns an unspecified element in set "s", or raises KeyError if empty
s.clear()
Removes all elements in set "s"
Please note: non-operator version of update() , intersection_update(), difference_update() and symmetric_difference_update() will accept any iterable as argument. Changes from version 2.3.1: Previously all parameters had to be sets.
Also please note: this module also contains a union_update() method, which is an alias of the update() method. This method is included for backward compatibility. Programmers should use the update() method more often, as this method is also supported by the built-in set() and frozenset() types.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Golang is better than Python in terms of performance and scalability. 1) Golang's compilation-type characteristics and efficient concurrency model make it perform well in high concurrency scenarios. 2) Python, as an interpreted language, executes slowly, but can optimize performance through tools such as Cython.

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".
