python basics-set collection
set set is an unordered and non-repeating set of elements
1.set creation
2 ways:
se = {11,22,33}
se = set([11,22,33]) #Call the __init__ method of set to create
2.Common methods of set
1.add
se = {11,22,33}se.add(44)
print(se) => {33,11,44,22} #Because it is unordered, the execution result will be different, but 44 is indeed added to the original set set
2.remove
se = {11,22,33 }
se.remove(11)
print (se) => {22,33}
se.remove(44) #Error report, prompting that the specified element cannot be found
3.discard
se = {11 ,22,33}se.discard(11)
print (se) => {22,33}se.discard(44)
print (se) => {11,22,33} #Cannot find the specified element, no deletion, no error
4.pop
se = {11,22,33}
se.pop()print (se) => {11,22} # Randomly pop an element from the stack , the execution results may be different ret = se.pop()print (ret) => {33} #Print the result of popping out the stack
5.difference
se1 = {11, 22, 33, 44}
se2 = {22, 33, 44, 55}
print(se1.difference(se2)) = > 11 # Print elements that exist in se1 but do not exist in se2 print(se2.difference(se1)) = > ; 55 #Print elements that exist in se2 but do not exist in se1
6.difference_update
se1 = {11,22,33,44}
se2 = {22,33,44,55}
se1.difference_update (se2)print (se1) => 11 #Overwrite the elements that exist in se1 and do not exist in se2 to se1, and update the set collection
7.intersection
se1 = {11,22,33,44 }
se2 = {22,33,44,55}
print (se1.intersection(se2)) => {22,33,44} #Se1, se2 intersection
8.intersection_update
se1 = { 11,22,33,44}
se2 = {22,33,44,55}
se1.intersection_update(se2)
print (se1) => {33,44,22} #Overwrite the intersection of se1 and se2 Write to the set of se1
9.union
se1 = {11,22,33,44}
se2 = {22,33,44,55}
print (se1.union(se2)) => {11,22,33,44,55} #se1, the union of se2

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

AI Hentai Generator
Generate AI Hentai for free.

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



Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

Regular expressions are powerful tools for pattern matching and text manipulation in programming, enhancing efficiency in text processing across various applications.
