Python notes (2): List + List Data Processing + functions, python Data Processing
# It is not easy to learn. It is inevitable that there are some inappropriate information. please correct me without your consideration. Thank you.
# Suitable for beginners.
List data is stacked from the bottom up (forming a stack), similar to arrays in other programming languages. For example:
User = ["James","Li Si","Wang Wu"]
Use [] brackets offset to access the list data:
Print (user [0]) Output: John
Print (user [1]) Output: Li Si
Print (user [2]) Output: Wang Wu
(1) list common methods:
Append (): adds a data item to the end of the list.
User = ["James","Li Si","Wang Wu"]
User. append ("Li Liu")
At this time, user = ['zhang san', 'Li si', 'wang wu', 'Li liu']
Extend (): adds a data item set at the end of the list.
User = ["James","Li Si","Wang Wu"]
User. extend (["James","Lili"])
At this time, user = ['zhang san', 'Li si', 'wang wu', 'xiaoming ', 'lily']
Pop (): deletes the data at the end of the list.
User = ["James","Li Si","Wang Wu"]
user.pop()
At this time, user = ['zhang san', 'Li si']
Remove (): remove a specific data.
User = ["James","Li Si","Wang Wu"]
User. remove ("James")
At this time: user = ["Li Si","Wang Wu"]
Insert (): Add a data item before the specified position.
User = ["James","Li Si","Wang Wu"]
User. insert (0,"Zhang Lin")
At this time: user = ['Zhang Lin','Zhang san','Li si','Wang wu']
(2) The list can store mixed-type data, such as the user name and year of birth.
User = ["James", 189,"Li Si", 188,"Wang Wu", 187]
(3) The list can be nested and support multi-layer nesting (up to 100 may be supported)Layer)
For example:
User = ["James", 189,"Li Si", 188,"Wang Wu", 187 ,["Zhang San's annual income",["Wage income","Allowance","Source heaven"]
["Zhang San's annual income",["Wage income","Allowance","Source heaven"] The entire data is used as the 6th items of the List user.
Access list data:
Print (user [0]) Output: John
Print (user [6]) Output :["Zhang San's annual income",["Wage income","Allowance","Source heaven"]
Print (user [6] [0]) output:Zhang San's annual income
Print (user [6] [1] [1]) output:Wage Income
(4) use for inIterative processing list.
User = ["James","Li Si","Wang Wu",["Zhang San's annual income",["Wage income","Allowance","Source heaven"]
# The for loop of python is used to process the list and other iteration structures.
# It can also be implemented through while, but the for loop is more convenient.
ForUeInUser:
Print (ue)
Output:
Zhang San
Li Si
Wang Wu
['Michael's annual income ', ['salary income', 'paid', 'From heaven ']
(5) 4thYou can find that the program only prints the list data of the outermost layer,['Zhang San's annual income',['Wage Income','Allowance','Source heaven']Is output as a list. How can I print the data in the inner list?
User = ["James","Li Si","Wang Wu",["Zhang San's annual income",["Wage income","Allowance","Source heaven"]
ForUeInUser:
# Isinstance() Is a built-in function, which is used to determine whether the data type is a list.
IfIsinstance (ue, list ):
ForUInUe:
Print (u)
Else:
Print (ue)
Output:
Zhang San
Li Si
Wang Wu
Zhang San's annual income
['Salary income ', 'allowance', 'source heaven ']
(6) in step 5, we can find that the List on the third layer is still printed in the form of a list. What should I do? Add anotherIfLoop? What if there are more layers? To make the code more concise, we can use functions for processing.
Function Format:
DefParameter Name ():
Function Code Group
() The parameters in brackets are optional. One or more parameters can be set, or no parameters can be set, but must be set in brackets.
Through the following function, we can process the list of multi-layer nesting.
#Create a function to process the list
DefPrint_li (the_list ):
ForUeInThe_list:
#Determines whether the data type is a list.
IfIsinstance (ue, list ):
Print_li (ue)
Else:
Print (ue)
User = ["James","Li Si","Wang Wu",["Zhang San's annual income",["Wage income","Allowance","Source heaven"]
#Call a function
Print_li (user)
Output:
Zhang San
Li Si
Wang Wu
Zhang San's annual income
Wage Income
Allowance
Source heaven