In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces how to use the set function in Python, the content is very detailed, interested friends can refer to, hope to be helpful to you.
The set collection function is mainly used to remove repetition:
For example, if a list has multiple duplicate values, you can get rid of it with set
> l = [1, 2, 3, 4, 5, 4, 4, 3, 21]
> > >
> > l
[1, 2, 3, 4, 5, 4, 3, 21]
> > >
> > >
Set (l)
Set ([1,2,3,4,5,21])
> > >
Set can do intersection, union and subtraction.
Addition, deletion and modification of set
Zeng: > a = set ([1Jing 2Jing 3Jing 4])
> > a
Set ([1,2,3,4])
> > >
> a.add ('alex')
> > a
Set ([1,2,3,4, 'alex'])
Delete: > a.remove ('alex')
> > a
Set ([1,2,3,4])
> > >
Add multiple items: > b = set ([1, 2, 7, 8])
> > b
Set ([8,1,2,7])
> > a.update (b)
> > a
Set ([1,2,3,4,7,8])
Determine whether b is included in a
> > a
Set ([1,2,3,4,7,8])
> > b
Set ([8,1,2,7])
> > >
> > b.issubset (a)
True
The "unordered" set in python is relative to other set implemented with balanced binary trees, such as set in C++ using red-black trees and orderedset in python.
Set access operations based on balanced binary tree are all O (lgn) time, but because of the characteristics of binary search tree, it is easy to find the precursor and successor nodes of any node, so it can be regarded as "orderly".
The set in python is implemented based on hash table, and the access time can be regarded as O (1), but there is no way to efficiently complete sequence-related operations (such as finding precursors and successors, maximum and minimum values, etc.), so it is considered to be "unordered".
On how to use the set function in Python to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.