CDA Data Analyst Produce
author :CDA The star lecturer Cao Xin
edit :Mika
01 Python And None No 0
Today we are going to talk about none Follow 0 The relationship between .
I also wrote the title very clearly ,none No 0 .
How can we verify this ? At the same time, we put none Follow 0 Put it on both sides of the equal sign , Let's take a look , It returns the result that false.
none In fact, it is a special null value , It can only be none is none yes true. and none be equal to none The time is true, therefore none No 0 , You must remember .
02 Python It traverses the dictionary in order
Let's talk about traversing dictionaries in order .
To tell the truth is a need for a more false proposition , But we still try our best to let everyone have a try .
I have a dictionary here a_dict , its key yes 2,3,1 , In fact, we can feel that it is not arranged in a certain order .
So if we print directly , its key The order of is still 2,3,1, If I want it to be 1,2,3 To sort , What should I do ?
Here I will introduce a method called sorted This built-in function .
That is, before you print , First you will a_dict Of keys First sorted Sort out , After sorting , He will sort in a default way , hold 1,2,3 Arrange the order for you , That's what we're going to get .
But what I want to tell you is , Actually, dictionaries have no order , Then we must specify a sort method for him , It is possible to print in sequence , If you meet in the future , Just remember one point , Dictionary it has no order .
03 Python To delete dictionary elements
Let's learn how to delete the elements of the dictionary .
Here I have a a_dict , Zhang San 、 Li Si 、 Wang Wu's height .
That way 1 It's using delete This method , Abbreviations are del , direct del Space a_dict, Specify the of the element you want to delete key, For example, Li Si .
Let me run it , First run the dictionary , then delete, No results returned .
Let's take another look a_dict When , I found that Li Si had been deleted .
The first 2 There are three ways we still need to a_dict Give me a copy , Because I have already deleted .
The first 2 One method is called pop , Its usage is a_dict.pop, Then specify the... Of this dictionary key.
Run it , You will find that it returns value , Follow the one above del There is a difference , But the end result is the same , Also delete this element .
Have you learned these two methods ?