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

Analysis of collection examples in Python built-in data types

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

Share

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

In this article Xiaobian for you to introduce in detail the "Python built-in data types in the collection instance analysis", detailed content, clear steps, details handled properly, I hope that this "Python built-in data types in the collection instance analysis" article can help you solve doubts, following the editor's ideas slowly in-depth, together to learn new knowledge bar.

1. Introduction to Collective set

The biggest feature of a collection is that each element is unique, it can delete, add, or replace elements by adding or deleting, but it has no subscript and you cannot access the collection through the subscript or slicing function. Because the collection is like a bag containing glass balls of different colors, you can replace the glass balls, but you cannot specify where to store the glass balls.

Its greatest function is that it is unique to its elements, and it is often used to remove excess data.

1.1 create a collection directly using {}

You can use {} to create a collection directly, but you cannot use {} to create an empty collection because {} is used to create an empty dictionary. You can only create an empty collection through set ().

1.2 create a collection using the set () function

Using the built-in function set, you can quickly convert other types of iterable objects into collection objects. These iterable objects can be strings, lists, tuples, and so on.

Print ("create an empty set") print (set ()) str1 = 'ABCDEFunctionsconverting collection (str1) print ("*" * 40) print ("a collection converted by a string, each element is a unique individual character") print (s) list1 = [0Mei 1Magi 2J 43J 2J 1] s=set (list1) print ("*" * 40) print ("A set converted by a list, but elements repeated in the list only appear once") print (s) 2. The collection does not have slicing function!

Please understand carefully what it means that the collection does not have slicing, which means that it cannot be sorted (of course, it can be converted to a list and then sorted), which means that you cannot use the while loop with the subscript to get the elements in turn, which you can get with the for loop.

3. Common built-in functions for handling collections 3.1 len functions

The len function returns the length of the collection, which is the number of elements.

3.2 map function

The map function does the same for each element in the collection, then returns a map object, and then uses the set function to get the collection.

> a {'sdf',' er'} > set (map (lambda XRV x + "-", a)) {'er-',' sdf-'} 3.3 filter function

The filter function does the same for each element in the collection, and only the eligible elements remain and return an object of type filter.

> a {'sdf',' er'} > set (filter (lambda x:len (x) = = 2)) {'er'} 3.4 reduce function

The filter function iterates over the elements in the collection in turn to get an object.

> import functools > a {'sdf',' er'} > functools.reduce (lambda x Magi yer' x + "~" + y Magi a) 'sdf~er'3.5 sum function

The sum function, which is valid only for collections where elements are numeric, returns the sum of the elements in the list, or TypeError if the element type is not a numeric value.

3.6 max function

The max function, which returns the maximum value of the element in the collection.

3.7 min function

The min function, which returns the minimum value of elements in the collection.

> a = {1 sum (a) 6 > min (a) 1 > max (a) 3 > 3.8 sorted function

The sorted function sorts the collection in the specified way. Note that using the sorted function results in a sorted list, not a collection! The original collection does not change.

List1 = [s=set (list1) s1=sorted (s) print (S1) print (type (S1))

3.9 enumerate function

The enumerate function can take out a collection in turn.

3.10 any function

The any () function determines whether all elements of a given collection are False, returns False, and returns True if one is True.

3.11 all function

The all () function determines whether all elements of a given collection are TRUE, and returns False if it returns True.

4. The built-in method of the collection 4.1. add (val)

The .add (val) method appends an element val, and if the val is already in the collection, the original collection remains unchanged.

4.2. Clear ()

The .clear () method empties the collection.

4.3. Copy ()

The .copy () method returns a copy of the collection (instead of sharing the same address as the original list).

4.4.difference (set1,set2,...)

The difference () method is used to return the difference of a collection, that is, the returned collection elements are contained in the first collection, but not in the second, third, and N sets (the parameters of the method).

4.5.difference _ update (set1,set2,...)

The difference_update () method is used to remove elements that exist in two or more collections.

The difference between the difference_update () method and the difference () method is that the difference () method returns a new collection that removes the same element, while the difference_update () method removes the element directly from the original collection without returning a value.

The use of # collections a = {1, 2, 2, 3, 4, 1, b = {4, 5, 6, 7, # 1. A collection is a print ("1.", a) # 2 with no duplicate elements. The collection uses the add method to add an element a.add ("6") print ("2.", a) # 3. The collection uses the clear method to clear a.clear () print ("3.", a) a = {1, 2, 3, 4} # 4. The collection uses the copy method to copy data c=a.copy () print ("4.", c) # 5. The collection uses the difference method to return data print ("5.", a.difference (b)) # 6 that is different from other collections. The collection uses the difference_update method to delete # the same data as other collections # a = {1 a.difference_update (b)) print ("6.", a) a = {1} a.difference_update (BJC) print ("6.", a). The collection uses the discard method to delete the data vala= {1 a.discard 2 3jue 4} a.discard (2) print ("7.", a)

4.6.discard (val)

The .discard method deletes the specified element in the collection.

4.7.Intersection (set1,set2,...)

The .intersection method returns the intersection of the collection and is not affected by itself.

4.8.Intersection _ update ()

The .intersection method changes itself to the intersection of two or more collections in place.

> a = {1, 2, 4, 4, 5, 5} > > b = {2, 3, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5

The .isdisjoint method determines whether two collections contain the same element, and returns False if no True is returned.

> a = {1 False > b = {2 True4.10 3, 4, 5, 7} > > c = {"a", "b"} > a.isdisjoint (b) True4.10 > a.isdisjoint (c)

The .issubset method determines whether its own set is a subset of the method parameter collection other_set.

> a = {1 False 2 > b = {2 True4.11 3, 4, 5, 7} > > c = {1} > a.issubset (b) False > a.issubset (c) False > c.issubset (a) True4.11 .issuperset (other_set)

The issuperset method determines whether the method parameter set other_set is a subset of its own set.

> a = {1, False 2, 3, 4, 5} > > b = {2, 3, 4, 5, 6, 7} > > c = {1} > a.issuperset (b) False > a.issuperset (c) True4.12. Pop ()

The .pop () method returns an element in the collection, which is deleted by the original collection.

Notice that the pop method in the collection has no index parameter! If the index parameter is entered using the pop method in the list, a TypeError: pop () takes no arguments (1 given) error is returned.

4.13. Remove ()

The. Remove (val) method removes the val element from the collection and returns it empty. If val is not in the collection, a KeyError error is returned.

4.14. Symmetric _ difference ()

Returns a collection of elements that do not repeat in two collections without changing their own collection.

4.15. Symmetric _ difference_update ()

Removes elements in the current collection that are the same in another specified collection and inserts different elements in another specified collection into the current collection.

# 10. The collection returns # different data in the two sets using the ^ operator # is equivalent to the symmetric_difference method a = {1 a.symmetric_difference (b) = 7} tmp= a ^ bprint ("10.", tmp) print ("a.symmetric_difference = >", a) print ("bounded = >", b) print ("the original set is not affected by using the ^ operator") print ("*" * 60) print ("a.symmetric_difference (b) = >", a.symmetric_difference (b) print ("averse = >") A) print ("bounded = >", b) print ("the original set using the symmetric_difference method is not affected") print ("*" * 60) print ("a.symmetric_difference_update (b) = = >", a.symmetric_difference_update (b) print ("changing the original set using symmetric_difference_update") print ("print = >", a) print ("bounded = >", b) print ("*" * 60)

4.16. Union (other_set)

The .union method returns the union of two sets without changing its own collection.

4.17. Update (other_set)

The .update method adds elements to its own collection.

> > a = {0ret 1} > b = {3 rect 4} > > c = {5 rect 6} > > a.union (b) {0rect 1, 3 recorder 4} > > a {0rect 1} > > b {3rect 4} > a.update (b) > > a {0prit 1,3 recorder 4} > > b {3rect 4} 5. Collection and operator 5.1-operator

An and b are two sets; a-b is similar to the a.difference (b) method, that is, it returns elements contained in set a but not in set b, and does not change itself.

> a = {0rect 1pr 2je 3} > > b = {2pje 3je 4je 5} > > a muri b {0jue 1} > a.difference (b) {0jue 1} 5.2 | operator

An and b are two sets; a | b is similar to the a.union (b) method, which returns all the elements in sets an and b without changing itself.

> a = {0flint 1, 2, 2, 3} > b = {2, 1, 4, 4, 5, 5, 5, 3, 4, 5, 5, 3, 4, 4, 5, 5, 5, 3, 4, 5, 5, 5, 3, 4, 4, 5, 5, 5, 3, 4, 4, 5, 5, 3, 4, 4, 5, 5, 5, 5, 5, 3, 4, 4, 5, 5, 5, 5, 5, 5, 3, 4, 4, 5, 5, 5, 3, 4, 4, 5, 5, 5, 5, 3, 4, 4, 5, 5, 5, 3, 4, 4, 5, 5, 5, 5, 3, 4, 4, 5, 5, 5, 3, 4, 5, 5, 5, 5, 3, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 3, 4

An and b are two sets; a & b is similar to the a.intersection (b) method, which returns elements contained in both sets an and b without changing themselves.

# 9. Set use & select overlapping parts of the collection # is equivalent to insection method a = {1, 2, 3, 4} b = {4, 5, 6, 7} print ("* initial data *") print ("initial data = >", a) print ("bounded = >", b) print ("8. A & baked = >", a & b) print ("after executing a & b") print ("after executing a & b") print ("baked = >", a) print ("baked = >") B) print ("using the & operator is not affected") print ("*" * 60) print ("8. A.intersection (b) = = >", a.intersection (b)) print ("using the intersection method is not affected") print ("a collection = >", a) print ("bounded = >", b) print ("*" * 60) print ("8. A.intersection_update (b) = = >" A.intersection_update (b)) print ("the original set will change using the intersection_update method") print ("aversion = >", a) print ("breadth = >", b) print ("*" * 60)

5.4 ^ operator

An and b are two sets; a ^ b is similar to the a.symmetric_difference (b) method, that is, it returns elements contained in an and b in different times without changing itself.

> a = {0 in 3} > > b = {2 in 3} > > b = {2 recorder 3, 4 recorder 5} > b (b) {0 recorder 1, 4 recorder 5} > > a ^ b {0 recorder 1, 4 recorder 5} 5 5 in, not in operator

The in operator determines that an element belongs to a collection

The not in operator determines that an element does not belong to a collection

> a = {0ret 1 in aTrue 3} > 0 in aTrue > "0" in aFalse, this article "Collection instance Analysis in Python built-in data types" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, 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.

Share To

Development

Wechat

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

12
Report