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

What are the collective knowledge points of python?

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces "what are the collection knowledge points of python". In daily operation, I believe that many people have doubts about the collection of knowledge points of python. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "what are the collection knowledge points of python?" Next, please follow the editor to study!

Definition of set

A dict is a set of 0 to more unordered, non-repeating immutable objects. The underlying structure of the collection is also a hash table (hash table), which can quickly find the elements in the collection. It is a data structure that trades space for time.

Set1 = {'Today', 'headlines', 'about', 'collection', 'case'} for i in set1: print (I) # run the above code verification many times, you can find that the order of printing is different each time, indicating that it is out of order. Creation of a collection

Empty collection, set ().

Set1 = set () print (type (set1)) out:

A collection of 1 elements, surrounded by curly braces.

Set1 = {'Today'} print (type (set1)) out:

A collection of multiple elements, surrounded by curly braces and separated by commas.

Set1 = {'Today', 'headlines', 'about', 'collection', 'cases'} for i in set1: print (I) out: headlines about the collection of cases today

Other types are converted to collections.

Tupl1 = ('name',' age', 'money',' height') list1 = ["Mike", 20,180] dict1 = {'name':' Mike', 'age': 20,' money': 8000, 'height': 180} set1 = set (tupl1) set2 = set (list1) set3 = set (dict1) print (set1, type (set1) print (set2, type (set2)) print (set3, type (set3) out: {' money', 'money',') 'age'} {8000, 180,' Mike', 20} {'money',' height', 'name',' age'}

The method of creation is the same as that of list derivation.

Set1 = {i for i in range (10)} print (set1, type (set1)) out: access to {0,1,2,3,4,5,6,7,8,9} collections

The collection is special and there is no direct access to the element. However, you can loop through the elements over the collection for.

Set1 = {'headline', 5, 4, 3, 'set', 2, 1, 'test'} for i in set1: print (I) out: headline 23451 increase in test sets

Add elements through the add method. The parameters of add can only receive immutable objects (numbers, strings, tuples), not mutable objects (lists, dictionaries, collections).

Set1 = {'headline', 3, 'collection', 2, 1, 'test'} set1.add (('abc','bcd')) print (set1) out: {1, 2, 3,' headline', 'set', 'test', ('abc','bcd')}

Add through the update method. The parameters of a update can be either immutable or mutable (an error will be reported if a mutable object is nested).

Set1 = {'headlines', 3, 'collection', 2, 1, 'test'} set2 = [[I, j] for i in range (5,8) for j in range (8,10)] # set1.update (set2) reported an error because the internal element of set2 is a list and cannot be converted to an immutable object set3 = [i for i in range (5,8)] set1.update (set3) # successful because the internal element of set3 is a number Can be converted to immutable object print (set1) out: {1, 2, 3, 'test', 5, 6, 7, 'headline', 'collection'} collection deletion

Del: you can delete the collection itself directly, but you cannot specify the elements of the delete collection.

Set1 = {'headlines', 3, 'collections', 2, 1, 'test'} del set1

Pop method: a no-parameter method that randomly deletes an element and returns.

Set1 = {'headlines', 3, 'Collection', 2, 1, 'Test'} print (set1.pop ()) out: collection

Remove method: removes the specified element. If the element does not exist, an error will be reported and no value will be returned.

Set1 = {'headlines', 3, 'collections', 2, 1, 'test'} set1.remove ('headlines') print (set1) # set1.remove (5) 5b is not an element of set1, and KeyErrorout: {1, 2, 3, 'collection', 'test'} is reported when removed.

Discard method: removes the specified element. If there is no error in the element, no value is returned.

Set1 = {'headline', 3, 'collection', 2, 1, 'test'} set1.discard (5) set1.discard (1) print (set1) out: {2, 3, 'set', 'headline', 'test'}

Clear method: clear the contents of the collection, keep the collection itself, and use this method first when you need to empty the collection in the body of the loop, rather than creating a new collection, because it is relatively expensive.

Set1 = {'headlines', 3, 'collection', 2, 1, 'test'} set1.clear () print (set1) out: {} set operation

Union: all elements of set An and set B.

Set_a = {0,1,2,3,4} set_b = {3,4,5,6,7} set_c = set_a.union (set_b) print (set_c) out: {0,1,2,3,4,5,6,7}

Intersection: elements common to sets An and B.

Set_a = {0,1,2,3,4} set_b = {3,4,5,6,7} set_c = set_a.intersection (set_b) # returns the intersection of sets an and b set_a.intersection_update () # directly removes the elements in the set a that are not intersecting print (set_c) print (set_a) out: {3,4} {3,4}

Difference: an element that exists in set A but not in set B.

Set_a = {0,1,2,3,4} set_b = {3,4,5,6,7} set_c = set_a.difference (set_b) # returns the difference between sets an and b set_a.difference_update (set_b) # directly removes print (set_c) print (set_a) out: {0,1,2} {0,1,2}

Equivalent difference: the union of sets An and B then removes the intersection of sets An and B.

Set_a = {0,1,2,3,4} set_b = {3,4,5,6,7} set_c = set_a.symmetric_difference (set_b) # returns the equivalent difference set_a.symmetric_difference_update (set_b) of sets an and b. Change the set a to the equivalent difference print (set_c) print (set_a) out of sets an and b: {0,1,2,5,6,7} {0,1,2,5,6) 7} query for collection

Do in operations on the elements of the collection.

Set1 = {'headlines', 3, 'Collection', 2, 1, 'Test'} print (5 in set1) print (1 in set1) out:FalseTrue

Intersection or not: isdisjoint method to determine whether sets An and B have intersected, return False, not True.

Set_a = {0,1,2,3,4} set_b = {3,4,5,6,7} set_c = {8,9,10} print (set_a.isdisjoint (set_b)) print (set_a.isdisjoint (set_c)) out:FalseTrue

Whether or not a subset: the issubset method determines whether set An is a subset of set B and returns True instead of False.

Set_a = {3,4} set_b = {3,4,5,6,7} set_c = {8,9,10} print (set_a.issubset (set_b)) print (set_c.issubset (set_b)) out:TrueFalse

Whether or not the parent set: the issuperset method determines whether set An is the parent set of set B and returns True instead of False.

Set_a = {3,4} set_b = {3,4,5,6,7} set_c = {8,9,10} print (set_b.issuperset (set_a)) print (set_b.issuperset (set_c)) other methods of out:TrueFalse collection

The collection has a wealth of built-in methods, including len and copy in addition to the methods mentioned above.

Len, which returns the element length of the collection.

Copy, create a copy of the collection, the content is consistent, id is different. (note: deepcopy should be used for nested structures).

The following is an example:

Set_a = {0,1,2,3,4} print (len (set_a)) set_b = set_a.copy () print (id (set_a), id (set_b)) Special usage of out:52269718817472 2269718817024 collection

The collection has a deduplication function, which is very powerful. With regard to the use of set deduplication function, I would like to cite two special uses:

One line of code determines whether there are duplicate elements.

List1 = [1,1,2,3,4,5] list2 = [1,2,3,4,5] 6] str1 = "aabbcdefg" str2 = "abcdefghi" print (len (set (list1)) = = len (list1)) # unequal means repetitive print (len (set (list2)) = = len (list2)) # equal means no repetitive print (len (set (str1)) = len (str1) # unequal means repetitive print (len (set (str2)) = len (str2)) # equal means no repetition out:FalseTrueFalseTrue

Two lines of code implement de-duplication and maintain the original order.

List1 = [1, 1, 2, 3, 4, 5] list2 = list (set (list1)) list2.sort (key=list1.index) print (list2) out: [1, 2, 3, 4, 5] at this point, the study of "what are the collective knowledge points of python" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical 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

Internet Technology

Wechat

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

12
Report