In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to operate collections in Python". In daily operation, I believe many people have doubts about how to operate collections in 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 for you to answer the questions of "how to operate collections in Python". Next, please follow the editor to study!
Catalogue
1.
two。 Access the elements of the collection
3.
4.
5. Deletion of collections
1. Use the remove method
two。 Use the pop method
3. Use the discard method
6. Intersection and union of sets
1. Intersection
two。 Union set
7. Public method
8.python built-in function
1.len
2.max
3.min
4.del
1. Introduction to the collection
The collection is unordered, the elements in the collection are unique, and the collection is generally used to de-duplicate elements in tuples or lists.
Define an empty collection
The format is as follows:
Name=set ()
Note that the following is an empty dictionary, the default is a dictionary, if there is data judging according to the format
Name= {} print (type (name)) 2. Access the elements of the collection
Access through a loop
Name= {'xiaoming','xiaohong','xiaohua'} for i in name: print (I)
3. Addition of collections
Use the add method:
Name= {'xiaoming','xiaohong','xiaohua'} name.add (' xiaofei') print (name)
4. Modification of the collection
Use the update method
Split the incoming elements and pass them into the collection as individuals
Name= {'xiaoming','xiaohong','xiaohua'} name.update (' abc') print (name)
5. Deletion of the collection 1. Use the remove method name= {'xiaoming','xiaohong','xiaohua'} name.remove (' xiaohong') print (name)
Delete elements in the collection using remove if there is a direct deletion if there is no error reported by the program
Name= {'xiaoming','xiaohong','xiaohua'} name.remove (' fei') print (name)
two。 Use the pop method
Using pop to delete is to randomly delete elements in the collection if there are no elements the program reports an error
Name= {'xiaoming','xiaohong','xiaohua'} name.pop () print (name)
3. Use the discard method name= {'xiaoming','xiaohong','xiaohua'} name.discard (' xiaohua') print (name)
Delete using discard if the element exists, delete directly if the element does not exist, do nothing.
Name= {'xiaoming','xiaohong','xiaohua'} name.discard (' pppp') print (name)
6. Intersection
Take out the common elements in the two collections and use the & symbol
two。 Union set
Combine two sets into a set and deduplicate it, using the | symbol
Name= {'xiaoming','xiaohong','xiaohua'} name1= {' xiaohong','xiaolan'} print (name | name1)
7. Public method
8.python built-in function
1.len
Calculate the number of elements in the container
Name='xiaoming'age= [7 print 8 age 9] sex= ('male', 'female') kv= {'hh':12,'kk':13,'ll':14} aa= {6 print (len (name)) print (len (age)) print (len (sex)) print (len (kv)) print (len (aa))
2.max
Returns the maximum value of the element in the container
Age= [7 max 8 kv 9] kv= {'hh':12,'kk':13,'ll':14} aa= {6 max 2 Jing 5 Jing 4 kv 9} print (max (age)) print (max (kv)) print (max (aa))
3.min
Returns the minimum value of the element in the container
Age= [7 min 8 kv 9] kv= {'hh':12,'kk':13,'ll':14} aa= {6 min 2 Jing 5 Jing 4 kv 9} print (min (age)) print (min (kv)) print (min (aa))
4.del
Delete a variable
Kv= {'hh':12,'kk':13,'ll':14} age= [7pc8] del age [1] print (age) del kv [' kk'] print (kv)
At this point, the study on "how to manipulate collections in Python" is over. I hope to be able to solve your 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.
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.