Home Backend Development Python Tutorial Detailed introduction of python-set collection class method

Detailed introduction of python-set collection class method

Mar 20, 2017 am 11:11 AM
python

 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)

For example: s1=set([11,22,33,44,'Tom','tony',11,77,2.5,])

s1.pop()

Result: {33, 'Tom', 2.5, 11, 44, 77, 22}

remove(...)

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Can visual studio code be used in python Can visual studio code be used in python Apr 15, 2025 pm 08:18 PM

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.

How to run programs in terminal vscode How to run programs in terminal vscode Apr 15, 2025 pm 06:42 PM

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

Can vs code run in Windows 8 Can vs code run in Windows 8 Apr 15, 2025 pm 07:24 PM

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.

Is the vscode extension malicious? Is the vscode extension malicious? Apr 15, 2025 pm 07:57 PM

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.

Python: Automation, Scripting, and Task Management Python: Automation, Scripting, and Task Management Apr 16, 2025 am 12:14 AM

Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.

What is vscode What is vscode for? What is vscode What is vscode for? Apr 15, 2025 pm 06:45 PM

VS Code is the full name Visual Studio Code, which is a free and open source cross-platform code editor and development environment developed by Microsoft. It supports a wide range of programming languages ​​and provides syntax highlighting, code automatic completion, code snippets and smart prompts to improve development efficiency. Through a rich extension ecosystem, users can add extensions to specific needs and languages, such as debuggers, code formatting tools, and Git integrations. VS Code also includes an intuitive debugger that helps quickly find and resolve bugs in your code.

Can vs code run python Can vs code run python Apr 15, 2025 pm 08:21 PM

Yes, VS Code can run Python code. To run Python efficiently in VS Code, complete the following steps: Install the Python interpreter and configure environment variables. Install the Python extension in VS Code. Run Python code in VS Code's terminal via the command line. Use VS Code's debugging capabilities and code formatting to improve development efficiency. Adopt good programming habits and use performance analysis tools to optimize code performance.

Can visual studio code run python Can visual studio code run python Apr 15, 2025 pm 08:00 PM

VS Code not only can run Python, but also provides powerful functions, including: automatically identifying Python files after installing Python extensions, providing functions such as code completion, syntax highlighting, and debugging. Relying on the installed Python environment, extensions act as bridge connection editing and Python environment. The debugging functions include setting breakpoints, step-by-step debugging, viewing variable values, and improving debugging efficiency. The integrated terminal supports running complex commands such as unit testing and package management. Supports extended configuration and enhances features such as code formatting, analysis and version control.

See all articles