In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
What are the common methods of Python collection? most people do not understand the knowledge points of this article, so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this article, "what are the common methods of Python collection?"
What is a collection?
A collection is not a type of data processing, but an intermediate type.
A set is an unordered, non-repeating sequence of elements.
Often used to deal with the intersection and difference of two lists.
Collections, like lists, support all immutable data types
All elements in the collection are placed in the middle of {} and separated by commas, for example:
{1, 2, 3}, a list of 3 integers
{'a', 'baked,' c'}, a list of 3 strings
The difference between set and list function list ordered unordered content repeatable function for the use of data access to the intersection, union and difference of data indexed unindexed symbols []-> [1, 2, 3] {}-> {1, 2, 3} collection creation method
Create a collection through the set function, or you can create it using curly braces with values. Such as {1, 2}, but cannot be created with empty curly braces. (the empty curly braces are an empty dictionary)
Examples are as follows:
Test_set_01 = set () #-> an empty collection test_set_02 = set ([1,2,3]) #-> incoming list or tuple test_set_03 = {1,2 3} #-> passing in the element test_set_04 = {} #-> this way is wrong This is an empty dictionary print (type (test_set_04)) # the result is as follows: #-- > test_list_01 = ['name',' age', 'birthday'] test_set_01 = set (test_list_01) print (test_set_01) # the result is as follows: # > {' name', 'age',' birthday'} you can see that the list is not passed in Instead, the element of the list test_list_02 = ['name',' age', 'birthday',' age'] test_set_02 = set (test_list_02) print (test_set_02) # the execution result is as follows: # > {'name',' age', 'birthday'} you can see the duplicate elements in the list Test_list_03 = (1, 2, 3, 1, 5) test_set_03 = set (test_list_03) print (test_set_03) # the result is as follows: # > > {1, 2, 3, 5} you can see the repeated elements in the tuple Deduplicated processing test_set_04 = {['name',' age', 'birthday']} print (test_set_04) # the execution result is as follows: # > TypeError: unhashable type:' non-transferable list within the list' collection Otherwise, an error will be reported: test_set_05 = {{'name',' age', 'birthday'}} print (test_set_05) # execution result is as follows: # > TypeError: unhashable type:' the dictionary test_set_06 = {'name', 1,3.14,666,888} print (test_set_06) # is also not allowed in the set' collection: # > {' name', 3.14,1, (666) Add, delete and modify add functions of collections with different data types (lists cannot be passed directly)
The function of the add function: it is used to add an element to the collection. If the added element already exists in the collection, the function will not be executed.
Usage of the add function: set.add (item); item is the element to be added to the collection; no return value.
Examples are as follows:
Test_set = {'name',' age', 'birthday'} test_set.add (' sex') test_set.add ('name') print (test_set) # the execution result is as follows: # > {' sex', 'birthday',' age', 'name'} existing' name' element, no add update function is executed again
The function of the update function: add a new collection (or list, tuple, string) to the collection, ignoring if the elements in the new collection exist in the original collection.
Usage of the update function: set.update (iterable); iterable is a collection, list, tuple, string; no return value, directly acting on the original collection.
Examples are as follows:
Test_set = set () test_list = ['name',' age', 'birthday'] test_set.update (test_list) print (test_set) # the execution result is as follows: # > the members (elements) of the {' birthday', 'age',' name'} list are added to the collection test_tuple = (666,888) test_set.update (test_tuple) print (test_set) # the execution result is as follows: # > > {'name' The members (elements) of the 'birthday',' age', 888,666} tuple are added to the collection name = 'Neo'test_set.update (name) print (test_set) # the result is as follows: # > > {' name', 'names,' birthday', 'egroups,' age', 'oaths, 888666} strings are remove functions in which each character is added to the collection as an element
The function of the remove function is to delete an element from the collection and report an error if the element does not exist.
Usage of the remove function: set.remove (item); item is an element in the current collection; no return value is returned and acts directly on the original collection.
It is important to note that item is an element in the collection, not an index.
Examples are as follows:
Test_set = set () test_list = ['name',' age', 'birthday'] test_set.update (test_list) print (test_set) # the execution result is as follows: # > {' name', 'birthday',' age'} test_set.remove ('age') print (test_set) # execution result is as follows: # > {' birthday' 'name'} test_set_01 = set () test_set.remove (' test') print (test_set_01) # the execution result is as follows: # > KeyError: an error is reported if the 'test'' test' element does not exist. Clear function
The function of the clear function: clear all elements in the current collection
The usage of the clear function: set.clear (); no return value, acting directly on the original collection.
Examples are as follows:
Test_set = set () test_list = ['name',' age', 'birthday'] test_set.clear () print (test_set) # the execution result is as follows: # > set () del method deletes the collection
Del function: delete directly (acts on collections, cannot be deleted by index)
Examples are as follows:
Test_set = set () test_list = ['name',' age', 'birthday'] test_set.update (test_list) print (test_set) # execution result is as follows: # > {' birthday', 'age',' name'} del test_setprint (test_set) # execution result is as follows: # > NameError: name' test_set' is not defined. An important note about the collection
The collection cannot get the element through the index
Collection does not have any method to get the element
Collection is only a temporary type used to process lists or tuples. It is not suitable for storing and transferring data.
Get the difference () function of the intersection, union, and difference of two sets
What is the difference set:
The sets of an and b, consisting of elements that belong to an and do not belong to b, are called the difference between a set and b set.
The function of the difference () function: returns the difference of a collection, that is, the returned collection elements are contained in the first collection, but not in the second collection.
The usage of the difference () function: a_set.difference (b_set); b_set is the set that the current set needs to be compared; the return value is the difference of the original set acting on the contrast set. (i.e. the difference between a_set and b_set)
Examples are as follows:
Names_set_01 = {'Neo',' Lily', 'Jack'} names_set_02 = {' Jack', 'Adem',' Albina'} names_diff = names_set_01.difference (names_set_02) print (names_diff) # the result is as follows: # > > {'Lily',' Neo'} intersection () function
What is intersection?
Two sets, an and b, respectively have the same set of elements, which is called the intersection of a set and b set.
The function of the intersection () function returns elements contained in two or more collections. (that is, intersection)
The use of the intersection () function: a_set.inersection (baked set...); b_set... One or more sets that need to be compared for the current set; the return value is the intersection of the original set acting on the contrast set.
Examples are as follows:
Names_set_01 = {'Neo',' Lily', 'Jack',' Adem'} names_set_02 = {'Jack',' Adem', 'Albina'} names_set_03 = {' Apollo', 'Ben',' Adem'} names_inter = names_set_01.intersection (names_set_02, names_set_03) print (names_inter) # the execution result is as follows: # > {'Adem'} union () function
What is union?
All elements in the an and b sets (excluding duplicates) are the union of the a set and the b set.
The function of the union () function: returns the union of multiple sets, that is, elements that contain all sets (duplicate elements appear only once)
The use of the union () function: a_set.union (baked set...); b_set... One or more sets that need to be compared for the current set; the return value is the union of the original set acting on the contrast set.
Examples are as follows:
Names_set_01 = {'Neo',' Lily', 'Jack',' Adem'} names_set_02 = {'Jack',' Adem', 'Albina'} names_set_03 = {' Apollo', 'Ben',' Adem'} names_union = names_set_01.union (names_set_02, names_set_03) print (names_union) # the execution result is as follows: # > {'Lily',' Neo', 'Albina',' Apollo' 'Jack',' Ben', 'Adem'} determines whether the same element isdisjoint () function exists in two sets
The function of the isdisjoint () function is to determine whether two sets contain the same element, and if no True is returned, False is returned
Usage of the isdisjoint () function: a_set.isdisjoint (b_set); b_set is the set used to judge from the current set; the return value is a Boolean value
Examples are as follows:
Names_set_01 = {'Neo',' Lily', 'Jack',' Adem'} names_set_02 = {'Jack',' Adem', 'Albina'} names_isdisjoint = names_set_01.isdisjoint (names_set_02) print (names_isdisjoint) # the execution result is as follows: # > Falsenames_set_01 = {' Neo', 'Lily',' Jack'} names_set_02 = {'Ben',' Adem' The execution result of 'Albina'} names_isdisjoint = names_set_01.isdisjoint (names_set_02) print (names_isdisjoint) # is as follows: # > True extension-the' &, |,-operator'of the collection
The & symbol can calculate the intersection of two sets, just as the intersection () function of the set does.
| the symbol can calculate the union of two collections, which is the same as the union () function of the collection.
The-operator can calculate the difference between two sets, just as the set's difference () function does.
The code example is as follows:
# intersection names_set_01 = {'Neo',' Lily', 'Jack',' Adem'} names_set_02 = {'Jack',' Adem', 'Albina'} names_set_03 = {' Apollo', 'Ben',' Adem'} names_inter = names_set_01.intersection (names_set_02 Names_set_03) names_inter_01 = names_set_01 & names_set_02 & names_set_03print (names_inter) print (names_inter_01) # the execution result is as follows: # > {'Adem'} # > > {' Adem'} # difference names_set_01 = {'Neo',' Lily', 'Jack'} names_set_02 = {' Jack', 'Adem' 'Albina'} names_diff = names_set_01.difference (names_set_02) names_diff_01 = names_set_01-names_set_02print (names_diff) print (names_diff_01) # the execution result is as follows: # > {' Lily', 'Neo'} # > > {' Lily', 'Neo'} # Union names_set_01 = {' Neo', 'Lily',' Jack', 'Adem'} names_set_02 = {' Jack', 'Adem' 'Albina'} names_set_03 = {' Apollo', 'Ben',' Adem'} names_union = names_set_01.union (names_set_02, names_set_03) names_union_1 = names_set_01 | names_set_02 | names_set_03print (names_union) print (names_union_1) # the execution result is as follows: # > > {'Adem',' Albina', 'Neo',' Jack', 'Lily',' Ben' 'Apollo'} # > > {' Adem', 'Albina',' Neo', 'Jack',' Lily', 'Ben',' Apollo'}
Differences between &, |,-and intersection (), union (), difference () functions
When performing a set operation:
The arguments to the intersection (), union (), difference () functions can be collections, lists, dictionaries, strings.
The left and right sides of the &, |,-operators must be collections.
The above is about the content of this article on "what are the common methods of Python collection?" I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please 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: 243
*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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.