In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
In this issue, the editor will bring you about the introduction and usage of collections in Python. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
I. the meaning of collection and the method of creating it
A set is a sequence that is unordered and contains different elements.
Collections can be created using curly braces {} or the set () function. Note: to create an empty collection, you must use set () instead of {}, because {} is used to create an empty dictionary.
Create collection a collection b
A = {1, 2, 3, 4, 5, 6}
B = {4, 5, 6, 7, 8, 9}
Second, the daily operation method of set
1. Remove duplicate elements from the list
Create a list c
C = [1, 2, 2, 3, 1, 1, 2]
Print (set (a))
Results:
{1, 2, 3}
two。 Add element
(1) add method
A = {1, abc', 3, 6, 6} b = {4, 5, 7, 7, 8, 9} a.add ("abc") print (a) results: {1, abc', 3, 4, 5, 5, 6, (2) update method
A = {1, 2, 3, 4, 5, 6} b = {4, 5, 7, 8, 9} a.update ("abc") print (a) results: {1, 2, 4, 5, 5, 6, 'cages,' bells,'a'}
Summary: add is the addition of a single element, and update is the addition of a sequence 3. Delete element (1) discard method
A = {1, a.discard, 2, 3, 4, 5, 6, 6) b = {4, 5, 6, 7, 8, 9} a.discard (5) print (a)
Results: {1,2,3,4,6} (2) remove method
A = {1, a.remove, 2, 3, 4, 5, 6, 6) b = {4, 5, 6, 7, 8, 9} a.remove (5) print (a)
Results: {1,2,3,4,6} (3) pop method
A = {1 print, 2, 3, 5, 5, 6} b = {4, 5, 7, 8, 8} a.pop () print (a) result: a summary of elements in an is randomly deleted: both remove and discard can delete specified elements. The difference is that if the deleted element does not exist, remove will report an error, and discard will not report an error. 4. Clear clear
A = {1, print 2, 3, 4, 5, 6) b = {4, 5, 6, 7, 8, 9} a.clear () 3 (a) result: set ()
5. Count the number of elements in the collection len
A = {1, 2, 3, 4, 5, 6} b = {4, 5, 7, 8, 9} print (len (a)) results: 6. The relationship between sets 1.The intersection of a set and b set
A = {1, e=a.intersection, 2, 3, 4, 5, 6, 6) b = {4, 5, 6, 7, 8, 9} e=a.intersection (b) print (e) or
A = {1, 2, 3, 4, 5, 6) b = {4, 5, 6, 7, 8, 9, print (a & b)
The result is the union of {4,5,6} 2.a set and b set.
A = {1, 2, 3, 4, 5, 6, 6) b = {4, 5, 6, 7, 8, 9} print (a.union (b)) or
A = {1, 2, 3, 4, 5, 5, 6} b = {4, 5, 5, 7, 7, 8, 9, 5, 5, 5, 5, 5, 5, 5, 5, 7, 7, 7, 7, 7, 7, 8, 7, 5, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, and 9} the symmetric difference between a set and b set (the elements after removing the intersection)
A = {1, 2, 3, 4, 5, 6, 6) b = {4, 5, 6, 7, 8, 9} print (a.symmetric_difference (b)) or
A = {1, 2, 3, 4, 5, 6) b = {4, 5, 6, 7, 8, 9, print (a ^ b) the results are all {1, 2, 3, 7, 8, 9} 4. Determine whether an is a subset of b ab
A = {1, 2, 3, 4, 5, 6, 6] b = {4, 5, 6, 7, 8, 9} print (a.issuperset (b)). Difference set (1) a Meib
# elements in a that are not in the b collection
A = {1, 2, 3, 4, 5, 6) b = {4, 5, 6, 7, 8, 9} print (a.difference (b)) results: {1, 2, 3} (2) bmera
# elements in b that are not in the a collection
A = {1, 2, 3, 4, 5, 6, 6] b = {4, 5, 6, 7, 8, 9} print (b.difference (a)) results: {8, 7
The above is the introduction and usage of the collection in Python shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
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.