In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you the "python how to achieve the set of add, delete and change operation", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to add, delete and change the collection of python operation" this article.
Addition, deletion and modification of the add function of the collection
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. The above is all the contents of this article "how to add, delete and modify the collection of python". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, 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.
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.