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

How to analyze the function of set set in python

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

Share

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

The content of this article mainly focuses on how to analyze the functions of the set set in python. The content of the article is clear and clear. It is very suitable for beginners to learn and is worth reading. Interested friends can follow the editor to read together. I hope you can get something through this article!

S = {x1 ~ x2 ~ x ~ 3.}

The set has the function of automatic deduplication, and the intersection, union and complement operation can be carried out, and the set is out of order, and the result of each print is different, so the value can not be taken by element subscript. A specific value cannot be modified because it cannot be taken. However, deletion can be performed.

S=set (); means to create an empty collection. Note: s = {} means to create an empty dictionary.

Common query

X in s is a collection that returns the bool type to determine whether x is in the set s.

Increase

S.add ("x"); add "x" to the collection s.

Delete

S.pop (); randomly delete an element in the collection s.

S.remove ("x"); delete the element "x" in the collection s and report an error if there is no "x".

S.discard ("x"); delete the element "x" in the collection s. If there is no "x", no error will be reported.

S.clear (); empty the collection s, and then s=set ().

Intersection, union, complement and symmetrical difference intersection

The set s is the intersection of set S1 and set S2. It is obtained that the set s is the intersection of set S1 and set S2.

Union set

S=s1 | s2transfers1.unoin (S2), s=set.union (S1 Magazine S2); get that set s is the union of set S1 and set S2.

Complement set

We get that the set s is an element in set S1 but not in set S2.

Symmetry difference

S = S1 ^ S2; s=s1.symmetric_difference (S2), s=set.symmetric_difference (S1 ^ S2); it is obtained that the set s is the sum of the elements in set S1 and only in set S2.

Other

S1=s.copy (); copy the data of the set s to the different addresses of the sets S1 and S1, and do not affect each other. (if S1s is used, the data of S1 is the same as that of s and the address is the same, and modifying s is equal to modifying S1)

S.difference_update (S1 ~ S2); assign the result of s-s1-s2 (complement) to s.

S.intersection_update (S1 ~ S2); assign the result (intersection) of s&s1&s2 to s.

S.isdisjoint (S1); returns True if the intersection of s and S1 is empty, otherwise returns False.

S.issubset (S1); returns True if s is a subset of S1.

S.issuperset (S1); returns True if S1 is a subset of s.

S.symmetric_difference_update (S1); there can be only one parameter in parentheses that assigns the result of s ^ S1 (symmetry difference) to s.

S.update (S1 and S2); assign the result (union) of s | S1 | S2 to s.

Thank you for your reading. I believe you have some understanding of "how to analyze the function of set set in python". Go and practice it. If you want to know more about it, you can follow the website! The editor will continue to bring you better articles!

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