


add(add elements)
name = set(['Tom','Lucy','Ben']) name.add('Juny')print(name)#输出:{'Lucy', 'Juny', 'Ben', 'Tom'}
clear(clear all elements)
name = set(['Tom','Lucy','Ben']) name.clear()print(name)#输出:set()
copy(copy set collection)
name = set(['Tom','Lucy','Ben']) new_name = name.copy()print(new_name)#输出:{'Tom', 'Lucy', 'Ben'}
difference(returns different elements in two or more sets and generates a new set)
A = set([2,3,4,5= set([3,4= set([2=
difference_update(Delete elements in A set that exist in B set . )
A = set([2,3,4,5]) B = set([4,5]) A.difference_update(B)print(A)#输出:{2, 3}
discard(remove elements)
n = set([2,3,4]) n.discard(3)print(n)#输出:{2, 4}
intersection(take the intersection and generate a new set)
n1 = set([2,3,4,5]) n2 = set([4,5,6,7]) n = n1.intersection(n2)print(n)#输出:{4, 5}
intersection_update(take the intersection and modify the original set)
n1 = set([2,3,4,5]) n2 = set([4,5,6,7]) n1.intersection_update(n2)print(n1)#输出:{4, 5}
isdisjoint(Judge intersection, return False if yes, True if no)
n1 = set([2,3,4,5]) n2 = set([4,5,6,7])print(n1.isdisjoint(n2))#输出:False
issubset(Judge subset)
A = set([2,3]) B = set([2,3,4,5])print(A.issubset(B))#输出:True#A是B的子集
issuperset(Judge parent set)
A = set([2,3]) B = set([2,3,4,5])print(B.issuperset(A))# 输出:True#B是A的父集
pop(randomly remove an element)
n = set([2,3,4,5]) n1 = n.pop()print(n,n1)# 输出:{3, 4, 5} 2
remove(remove the specified element)
n = set([2,3,4,5]) n.remove(2)print(n)# 输出:{3, 4, 5}
symmetric_difference(take the intersection and generate a new set)
A = set([2,3,4,5]) B = set([4,5,6,7])print(A.symmetric_difference(B))# 输出:{2, 3, 6, 7}
symmetric_difference_update (take the intersection, change the original set)
A = set([2,3,4,5]) B = set([4,5,6,7]) A.symmetric_difference_update(B)print(A)# 输出:{2, 3, 6, 7}
union (take the union, and generate a new set)
A = set([2,3,4,5]) B = set([4,5,6,7])print(A.union(B))# 输出:{2, 3, 4, 5, 6, 7}
update (take the union, change the original set) Collection)
A = set([2,3,4,5]) B = set([4,5,6,7]) A.update(B)print(A)# 输出:{2, 3, 4, 5, 6, 7}
The above is the detailed content of Give an example to illustrate the function of python3 set method. For more information, please follow other related articles on the PHP Chinese website!

InPython,youappendelementstoalistusingtheappend()method.1)Useappend()forsingleelements:my_list.append(4).2)Useextend()or =formultipleelements:my_list.extend(another_list)ormy_list =[4,5,6].3)Useinsert()forspecificpositions:my_list.insert(1,5).Beaware

The methods to debug the shebang problem include: 1. Check the shebang line to make sure it is the first line of the script and there are no prefixed spaces; 2. Verify whether the interpreter path is correct; 3. Call the interpreter directly to run the script to isolate the shebang problem; 4. Use strace or trusts to track the system calls; 5. Check the impact of environment variables on shebang.

Pythonlistscanbemanipulatedusingseveralmethodstoremoveelements:1)Theremove()methodremovesthefirstoccurrenceofaspecifiedvalue.2)Thepop()methodremovesandreturnsanelementatagivenindex.3)Thedelstatementcanremoveanitemorslicebyindex.4)Listcomprehensionscr

Pythonlistscanstoreanydatatype,includingintegers,strings,floats,booleans,otherlists,anddictionaries.Thisversatilityallowsformixed-typelists,whichcanbemanagedeffectivelyusingtypechecks,typehints,andspecializedlibrarieslikenumpyforperformance.Documenti

Pythonlistssupportnumerousoperations:1)Addingelementswithappend(),extend(),andinsert().2)Removingitemsusingremove(),pop(),andclear().3)Accessingandmodifyingwithindexingandslicing.4)Searchingandsortingwithindex(),sort(),andreverse().5)Advancedoperatio

Create multi-dimensional arrays with NumPy can be achieved through the following steps: 1) Use the numpy.array() function to create an array, such as np.array([[1,2,3],[4,5,6]]) to create a 2D array; 2) Use np.zeros(), np.ones(), np.random.random() and other functions to create an array filled with specific values; 3) Understand the shape and size properties of the array to ensure that the length of the sub-array is consistent and avoid errors; 4) Use the np.reshape() function to change the shape of the array; 5) Pay attention to memory usage to ensure that the code is clear and efficient.

BroadcastinginNumPyisamethodtoperformoperationsonarraysofdifferentshapesbyautomaticallyaligningthem.Itsimplifiescode,enhancesreadability,andboostsperformance.Here'showitworks:1)Smallerarraysarepaddedwithonestomatchdimensions.2)Compatibledimensionsare

ForPythondatastorage,chooselistsforflexibilitywithmixeddatatypes,array.arrayformemory-efficienthomogeneousnumericaldata,andNumPyarraysforadvancednumericalcomputing.Listsareversatilebutlessefficientforlargenumericaldatasets;array.arrayoffersamiddlegro


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

Dreamweaver CS6
Visual web development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download
The most popular open source editor

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
