Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of set types of python

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report