Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of set types of python

Shulou Source: shulou.com Published: 2022-06-02 07:35:01 10月01日 Update

This article introduces the relevant knowledge of "python set type example analysis". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Collection:

A combination of disordered and unrepeatable elements

Use the collection to delete duplicates in the list (set () sing as a de-repetition operation)

Classification: mutable set (set), immutable set (frozenset)

Create a collection

Created with the set () function (mutable set) or the frozenset () function (immutable set)

Set ()

Frozenset ()

Immutable sets are hashable, and variable sets are hashable.

The basic operation of the collection

-: for difference complement (discrete relative complement) x belongs to A murb, which is equivalent to x belongs to A but x does not belong to B.

^: symmetric difference (discrete is called symmetric difference)

Example:

Operators can be combined

Built-in functions and methods of sets

Len (): returns the number of collections

Facing all collections:

S.issubset (t): determine whether s is a subset of t

S.issuperset (t): judge whether s is a superset of t

S.union (t): returns a new collection, which is the union of s and t

S.intersection (t): returns a new collection, which is the intersection of s and t

S.difference (t): returns a new collection, which is a set of members belonging to s but not t (difference complement)

S.symmetric_difference (t): returns a new set, which is a symmetric difference between s and t

S.copy () returns a copy of s

Built-in functions for variable sets

S.update (t): modify the s collection to include members of s and t union

S.intersection_update (t): modify the s collection so that s contains members of the intersection of s and t

S.difference_update (t): modify the s collection so that it contains members of Smurt

S.symmetric_difference_update (t): modify the s set so that it contains members of s and t symmetric difference sets

S.add (obj): add object obj to collection s

S.remove (obj): deletes the object obj from s, and generates a keyError exception if obj does not belong to s

S.discard (obj): removes the object Obj from s. If Obj does not belong to s, there is no action.

S.pop (): delete any member from s and return this member

S.clear (): empty the members in s

This is the end of the content of "sample Analysis of set types of python". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Member variable symmetry function package new set object type analysis intersection content more knowledge hash discrete practical learned next number element Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Linux macOS Shulou Information vpn