Python notes (ii): List + list data processing + functions

Source: Internet
Author: User
Tags ming

#才疏学浅, there are inevitably inappropriate, please do not hesitate to correct, thank you.

#适合初学者.

The data from the list is stacked up from the bottom up (forming a stack), similar to an array of other programming languages. For example:

user = [" Zhang San"," John Doe"," Harry"]

Harry

John doe

Tom

Use the [] bracket offset to access the list data:

Print (user[0]) output: Zhang San
Print (user[1]) output: John Doe
Print (user[2]) output: Harry

(a) A list of commonly used methods:

Append (): Adds a data item at the end of the list.
user = [" Zhang San"," John Doe"," Harry"]
User.append (" li Stay")
At this point, user = [' Zhang San ', ' John Doe ', ' Harry ', ' Li Stay ']
Extend (): Add a collection of data items at the end of the list
user = [" Zhang San"," John Doe"," Harry"]
User.extend ([" Xiao Ming"," Xiao Li"])
At this time, user = [' Zhang San ', ' John Doe ', ' Harry ', ' Xiao Ming ', ' Xiao Li ']
Pop (): Deletes the data at the end of the list.
user = [" Zhang San"," John Doe"," Harry"]
User.pop ()
At this point, user = [' Zhang San ', ' John Doe ']
Remove (): Removes a specific data.
user = [" Zhang San"," John Doe"," Harry"]
User.remove (" Zhang San")
At this point: User = [" John Doe"," Harry"]
Insert (): Adds a data item before the specified position.
user = [" Zhang San"," John Doe"," Harry"]
User.insert (0," Zhang Lin")
 At this point: User = [' Zhang Lin ' Zhang San ' John Doe' Harry ']

(ii) the list can hold mixed types of data, such as the name of the user and the year of birth

user = [" Zhang San", 189," John Doe", 188," Harry", 187]

(c) lists can be nested lists, supporting multiple layers of nesting (up to layer)

For example:
user = [" Zhang San", 189," John Doe", 188," Harry", 187,["  Zhang San Annual income ", [" wage Income "," allowances ", " Source Heaven " ]]]
[ " Zhang San Annual income", [" wage Income"," allowance", " source Heaven"]] The entire 6th item of data as a list user.
To access the data for the list:
Print (user[0]) output: Zhang San
Print (user[6]) output: [ " Zhang San annualincome", [" wage Income"," allowances","  Source of heaven "]
Print (user[6][0]) output: annual income of Zhang San
Print (user[6][1][1]) output: Payroll Income

(iv) using the for in iterate through the list.

user = [" Zhang San"," John Doe"," Harry", [" Annual income of Zhang San", ["  wage income "," allowance "," Source Heaven "]]
#python的for循环就是用来处理列表和其他迭代结构
#也可以通过while实现, but the for loop is more convenient
User
PRINT (UE)
Output:
Tom
John doe
Harry
[' Zhang San's annual income ', [' Wage income ', ' allowances ', ' source of heaven ']

(v) 4th step, you can see that the program only prints the outermost list data, [ ' Zhang San's annual income ', [' wage income ', ' allowances ' , ' source heaven ' '] is output as a list, how to print the data of the inner-layer list?

user = [" Zhang San"," John Doe"," Harry", [" Annual income of Zhang San", ["  wage income "," allowance "," Source Heaven "]]
User
#isinstance () is a built-in function, used here to determine whether the data type is a list
Isinstance (ue,list):
Ue:
Print (U)
Else:
PRINT (UE)

Output:

Tom

John doe

Harry

Annual income of Zhang San

[' Wage income ', ' allowances ', ' source heaven ']

(vi) The fifth step can be found that the third layer of the list is still printed in the form of a list, what should be handled? Add an if loop again? What if there are more layers? For the code to be more concise, we can use the function to handle it.

The format of the function:

Parameter name ():
Function code Group

() The arguments in parentheses are optional, can have one or more, or they can be set without parameters, but parentheses must have.

We can handle multiple nested lists with the following function.

# Create a function to process the list
Print_li (the_list):
The_list:
# determine if the data type is a list
ISINSTANCE (UE, list):
PRINT_LI (UE)
Else:
PRINT (UE)
user = [" Zhang San"," John Doe"," Harry", [" Annual income of Zhang San" , [" wage Income"," allowance"," Source Heaven"]]
# calling functions
Print_li (user)

Output:

Tom

John doe

Harry

Annual income of Zhang San

Wage income

Allowance

SOURCE Heaven

Python notes (ii): List + list data processing + functions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.