Get the App
SLTechnology News&Howtos  ›  Development  › 

How to manipulate collections in Python

Shulou Source: shulou.com Published: 2022-06-03 09:33:47 10月04日 Update

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!

Tags: Elements methods intersection learning containers two functions dictionaries more formats programs symbols help practical maximum minimum next individual number writing Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Docker Huawei Microsoft Shulou Tech Info