Any program that deserves to be created will inevitably process the data. To reduce complexity, you can usually organize your data into lists.
Accessing list data by using notation in parentheses
First define a list of names and then use Print () to display the list on the screen ... Next, use Len () to get the number of data items in the list, and then access and display the value of the second data item:
>>> cast=["a","b","C"]>>>Print(CAST) ['a','b','C']>>>Print(len (cast))3>>>Print(cast[1]) b>>>
After you create the list, you can use the list method to add a data item (append () method) at the end of the list, or to delete the data from the end of the list (the Pop () method), and to add a data collection at the end of the list (using the Extend () method):
>>> Cast.append ("e")>>>Print(CAST) ['a','b','C','e']>>>Cast.pop ()'e'>>>Print(CAST) ['a','b','C']>>> Cast.extend (["F","g"])>>>Print(CAST) ['a','b','C','F','g']
Locate and delete a specific data item in the list (using the Remove () method), and then add a data item (using the Insert () method) in front of a specific location:
>>> Cast.remove ('g')>>>Print(CAST) ['a','b','C','F']>>> Cast.insert (0,'g')>>>Print(CAST) ['g','a','b','C','F']>>>
Working with List data
This usually requires an iterative processing list, in which an action is completed for each data item. Of course, it's possible to do it often (this is feasible but not scalable):
>>> letter=['a','b']Print (letter[0]) aprint(letter[1]) b
This code will get our expected results so that the list of data appears on the screen. However, if you later modify the code and add a letter to the list, the above code will not be able to achieve the expected, because the code does not mention the third item of data.
It's hard to just add a print () statement, right?
But what if the number of letters is increased? This increased print () statement will be very much, you do not want to do so, certainly will find ways to escape.
This will use the iteration.
Processing each list item is a common requirement, and all Python can do this easily by providing a built-in for loop. You can use the following code, which overrides the preceding code for a For loop:
>>> letter=['a','b']for in Letter : print(each_flick) AB
For loop processing of any size list
Python's for loop is designed to work with other iterations in the list and Python structure. Lists make Python a common iteration data structure, and it is best to use a for loop when iterating over a list:
Structure of the FOR loop: for Target identifier in list
List Processing code
For indicates that the loop starts and appears before the target identifier
The keyword in divides the target identifier from the list
The colon ":" is placed after the list name, indicating that the list processing code begins
The list processing code must be indented below the For loop
The list processing code is called a "group" (suite) by a Python programmer.
In addition to using for, there is a candidate method that can be used to write iterative code using the while loop.
Python code is case-sensitive.
Save a list in a list
As we've seen, lists can hold mixed types of data. But there's something better: The list can hold a collection of anything, or it can include other lists. Simply insert the inside list in the list as needed:
letters=['a','b','C', [ '1','2','3']
Then output:
>>> letters=['a','b','C',['1','2','3']]>>>Print(Letters) ['a','b','C', ['1','2','3']]
At this point you will find that you do not want to output the results, you need a for loop to process this list:
for inch Letters: Print (Each_item) abc['1"2" 3']
For this time the for only prints out the individual data items of the outer list, and the next layer of the nested list is printed as-is.
There is no problem with the For loop itself, but the loop code is not complete. For a perimeter list, the internal list is just one list item in the outer list. Here we need a mechanism to discover that one item in the list is actually another list and take appropriate action.
Find a list in a list
If an item is indeed a list, the list is processed before the next item in the outside list is processed. Determine when to do what can be done with if else mode.
We need a way to determine that the current list of processed items is a list. You can use a python built-in BIF(BIF (built-in functions) as the name implies, is the built-in Erlang function. They are often used to accomplish tasks that cannot be done with Erlang. For example, convert a list to a tuple or get the current time and date. A function that accomplishes these operations, which we call Bif)isinstance (), which allows you to check whether a particular identifier contains data of a particular type:
>>> name=['Wang','niu'>>> Isinstance (name,list) True>>> num_name=len (name)print(num_name)2> >> isinstance (num_name,list) False
Code Explanation: Create a short list and assign it to an identifier name.
Ask if name is a list.
Assigns a number to an identifier.
View the value of an identifier
Ask if Num_name is a list
Chapter Two: Python list