


Detailed introduction of python-set collection class method
s1=set([11,22,33,44,'Tom','tony',11,77,2.5,]) returns {11,22,33,44, 'Tom', 'tony', 77,2.5} (Note: What is returned is not a dictionary, it just tells you that this set contains these elements, so the order of the result elements returned each time may be different)
s2=set([11,22,33,44,'Tom','tony',11,55,66,]) returns {11,22,33,44,'Tom','tony ', 55,66} (Note: What is returned is not a dictionary, it just tells you that this set contains these elements, so the order of the result elements returned each time may be different)
add(. ..)
Add an element to a set.
This has no effect if the element is already present. (If the element already exists, adding has no effect. This means that set is a collection without duplicate elements.)
For example: s1=set([11,22,33,44,'Tom','tony',11,77,2.5,])
s1.add('jimi')
print(s1)
Result: {33, 'tony', 2.5, 'jimi', 11, 44, 77, 22, 'Tom'}( Note: set is an unordered set, and the order of the results may be different each time. )
difference(...)
Return the difference of two or more. sets as a new set. (Find the main difference set and generate a new set)
(i.e. all elements that are in this set but not the others.) (Note: s1.difference(s2) means to find the elements that are in s1 but not in s2, and generate a new set. s2.difference(s1) means to find out the elements that are in s2 but not in s1 elements and generate a new set)
For example: s1=set([11,22,33,44,'Tom','tony',11,77,2.5,])
s2=set([11,22,33,44,'Tom','tony',11,55,66,])
s3=s1.difference(s2)
s4=s2.difference(s1)
print(s3)
print(s4)
Result: {2.5, 77}
{ 66, 55}
difference_update(...)
Remove all elements of another set from this set. (Remove all elements of another set from this set. Note: s1.difference_update(s2) means to remove the elements in s1 that are common to both, and retain the elements that are not common. It is a modification of s1 rather than generating a new list. s2.difference_update(s1) means to remove the elements in s2 that are common to both, and retain the elements that are not common. It is a modification of s2 rather than generating a new list. )
For example: s1=set([11,22,33,44,'Tom','tony',11,77,2.5,])
s2=set([11 ,22,33,44,'Tom','tony',11,55,66,])
s1.difference_update(s2)
print(s1)
Result: {2.5, 77}
discard(...)
Remove an element from a set if it is a member. (Remove members (elements) in the set)
If the element is not a member, do nothing. (If there is no such member in the set, no operation will be performed, and no error will be reported, which is different from Remove.)
For example: s1=set([11 ,22,33,44,'Tom','tony',11,77,2.5,])
s1.discard(2.5)
Result: {33, 'Tom', 11, 44, 77, 'tony', 22}
intersection(...)
Return the intersection of two sets as a new set. (Generate the intersection of the two sets and generate a new list)
(i.e. all elements that are in both sets.)
For example: s1=set([11,22,33,44,'Tom','tony',11,77, 2.5,])
s2=set([11,22,33,44,'Tom','tony',11,55,66,])
s5=s1.intersection (s2)
s6=s2.intersection(s1)
print(s5)
print(s6)
Result: {33, 'Tom ', 11, 44, 22, 'tony'}
{33, 11, 44, 'tony', 'Tom', 22}
intersection_update(...)
Update a set with the intersection of itself and another. (The function is the same as intersection(...), but s1.intersection(s2) modifies the original set s1 when executed and does not generate a new one. Set)
For example: s1=set([11,22,33,44,'Tom','tony',11,77,2.5,])
s2=set( [11,22,33,44,'Tom','tony',11,55,66,])
s1.intersection(s2)
s2.intersection(s1)
print(s1)
print(s2)
Result: {33, 'Tom', 11, 44, 22, 'tony'}
{33, 11, 44, 'tony', 'Tom', 22}
isdisjoint(...)
Return True if two sets have a null intersection. (Determine whether the two sets have an intersection, if so, return False, if not, return True)
For example: s1=set([11,22,33,44,'Tom','tony',11,77,2.5,])
s2=set([100,50,500 ,])
print(s1.isdisjoint(s2))
Result: True
issubset(...)
Report whether another set contains this set. (To determine whether all elements in a set are in another set, s1.issubset(s2) means that every element in s1 is in s2. Pay attention to the difference from s1.issuperset(s2). )
For example: s1=set([11,22,33,44,'Tom','tony',11,77,2.5,])
s2=set([11,22,33 ,44,'Tom','tony',11,])
s3=([11,22,33,44,'Tom','tony',11,55,66])
s1.issubset(s2)
s1.issubset(s3)
print(s1.issubset(s2))
print(s1.issubset(s3 ))
Result: True
False
issuperset(...)
Report whether this set contains another set. (Judge whether all elements in a set In another set, s1.issubset(s2) means that every element in s2 is in s1. Note the difference with s1.issubset(s2) )
For example: s1=set( [11,22,33,44,'Tom','tony',11,77,2.5,])
s2=set([11,22,33,44,'Tom','tony ',11,])
s3=([11,22,33,44,'Tom','tony',11,55,66])
s1.issuperset(s2 )
s1.issuperset(s3)
print(s1.issuperset(s2))
print(s1.issuperset(s3))
Result :True
##pop(...)
Remove and return an arbitrary set element.(Randomly delete an element)
Raises KeyError if the set is empty.( If the set is empty, KeyError will be prompted when pop is executed)
Remove an element from a set; it must be a member. If there is no such member, keyError will be prompted, which is different from discard. )
For example: s1=set([11,22,33,44,'Tom','tony',11,77,2.5,])
s1.remove(2.5)
s1.remove('jimi')
Result: {33, 'Tom', 'tony', 11, 44, 77, 22}
KeyError: ' jimi'
symmetric_difference(...)
Return the symmetric difference of two sets as a new set. (Generate a new list, this list is a set of non-duplicate elements in the two lists, s1 .symmetric_difference(s2) represents a set of elements in s1 that are not in s2 and elements in s2 that are not in s1)
(i.e. all elements that are in exactly one of the sets.)
For example: s1=set([11,22,33,44,'Tom','tony',11,77,2.5,])
s2=set([11,22,33, 44,'Tom','tony',11,55,66,])
s4=s2.symmetric_difference(s1)
print(s4)
Result: {2.5, 66, 77, 55}
symmetric_difference_update(...)
Update a set with the symmetric difference of itself and another. (Modify a list, there are no duplicate elements in the two lists The set of s1.symmetric_difference(s2) represents the set of elements in s1 that are not in s2 and elements in s2 that are not in s1)
For example: s1=set([11,22,33,44 ,'Tom','tony',11,77,2.5,])
s2=set([11,22,33,44,'Tom','tony',11,55,66, ])
s1.symmetric_difference_update(s2)
print(s1)
Result: {2.5, 66, 77, 55}
union(. ..)
Return the union of sets as a new set. (Generate a new set. The changed list is a set of all members (elements) of the two lists. s1.union(s2) means that it contains s1, s2 A new set of all elements) (i.e. all elements that are in either set.)
For example: s1=set([11,22,33,44,'Tom','tony',11 ,77,2.5,])
s2=set([11,22,33,44,'Tom','tony',11,55,66,])
s3= s1.union(s2)
print(s3)
Result: {33, 2.5, 66, 'Tom', 11, 44, 77, 55, 22, 'tony'}
update(...)
Update a set with the union of itself and others. (Update one set to another set, s1.update(s2) means putting all elements in s2 into In s1, complete the update and modification of s1)
For example: s1=set([100,50,])
s2=set([11,22,33,44,'Tom ','tony',11,55,66,])
s1.update(s2)
print(s1)
Result: {'tony', 33, 66, 100, 'Tom', 11, 44, 50, 22, 55}
The above is the detailed content of Detailed introduction of python-set collection class method. For more information, please follow other related articles on the PHP Chinese website!

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.

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.

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.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.

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".
