Getting Started with Python [] 4-1 Python create a list, accessed by index list, in reverse access list, add / delete / replace elements

1. Create a list

Python built- in data type is a list: list.

list is an ordered set, which you can add and remove elements at any time.

For example, list the names of all the students in the class, it can be represented by a list:

 >>> ['Michael', 'Bob', 'Tracy']
 ['Michael', 'Bob', 'Tracy']

list is an ordered set of mathematical sense, that is, list the elements are arranged in the order.

Very simple list structure, in accordance with the above code, the direct use of [] are all the elements enclosed list is a list object.
Generally, we will list assigned to a variable, so that you can refer to the variable list by:

>>> classmates = ['Michael', 'Bob', 'Tracy']
>>> classmates   # 打印classmates变量的内容
['Michael', 'Bob', 'Tracy']

Because Python is a dynamic language, so the list of elements contained are not required must be of the same type of data, we can include a variety of data in the list:

>>> L = ['Michael', 100, True]

There is no one element of the list, is the empty list:

>>> empty_list = []


Task:
suppose there is a class three students: Adam, Lisa and Bart, their grades are 95.5,85 and 59, follow the names, scores, the name, the score ... order with a list expressed in terms of scores from high to low, then print it out.

From writing code:

L = ['Adam', 95.5, 'Lisa', 85, 'Bart', 59]
print L

2, according to the index access list

Since the list is an ordered collection, so we can use a list in descending expressed by scores of three students in the class:

>>> L = ['Adam', 'Lisa', 'Bart']

How do we get students to specify the name of N from the list as well?
The method is to get the list of specified elements by index.

Of particular note is the index from 0, that is, the index of the first element is 0, the second element of the index is 1, and so on.

Therefore, to print the name of the first students, with L [0]:

>>> print L[0]
Adam

To print the name of the second of the students, with L [1]:

>>> print L[1]
Lisa

To print the name of the third of the students, with L [2]:

>>> print L[2]
Bart

To print the name of the students of the fourth, with L [3]:

>>> print L[3]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range

It is given! IndexError means that the index is out of range, because the above list only three elements, effective index is 0,1,2.

So, using the index, it should be careful not to cross-border.




Task:
three students' scores can be represented by a list:

L = [95.5, 85, 59]

Please print out respectively according to the first index, second, third, and the test print L [3].

From writing code:

L = [95.5,85,59]
print L[0]
print L[1]
print L[2]

Here Insert Picture Description

3, reverse access list

We still use a list 3 classmates from high to low by score shows the class:

>>> L = ['Adam', 'Lisa', 'Bart']

At this time, the teacher said, please lowest score students stand out.

Write the code to accomplish this task, we can first count this list, see that it contains three elements, therefore, the last element of the index is 2:

>>> print L[2]
Bart

Is there an easier way?

Have!

Bart students is the last one, commonly known as the last place, so we can use this index to -1 is the last element:

>>> print L[-1]
Bart

Bart students said lying gun.

Similarly, the penultimate expressed by -2, -3 represented by the third last, fourth to last -4 represented by:

>>> print L[-2]
Lisa
>>> print L[-3]
Adam
>>> print L[-4]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range

L [-4] error, because there is no fourth from the bottom, a total of only three elements.

When using the inverted index, should also be careful not to cross-border.

Task:
three students' scores can be represented by a list:

L = [95.5, 85, 59]

Please print out the last first, respectively, second to last, third from the bottom in accordance with the inverted index.


From writing code:

L = [95.5, 85, 59]
print L[-1]
print L[-2]
print L[-3]

Here Insert Picture Description

4, list to add a new element

Now, there are three students in the class:

>>> L = ['Adam', 'Lisa', 'Bart']

Today, the class transferred to a new classmate Paul, how to add new students to the existing list in it?

  • The first way is to use the list append () method, the new students added to the end of the list:
>>> L = ['Adam', 'Lisa', 'Bart']
>>> L.append('Paul')
>>> print L
['Adam', 'Lisa', 'Bart', 'Paul']

append () always adds a new element to the end of the list.

If Paul students said they always test score requirements added to the first position, how do?

the way is

  • A list of the insert () method, which accepts two parameters, the first parameter is the index number and the second parameter is the new element to be added:
>>> L = ['Adam', 'Lisa', 'Bart']
>>> L.insert(0, 'Paul')
>>> print L
['Paul', 'Adam', 'Lisa', 'Bart']

L.insert (0, 'Paul') means, 'Paul' is added to the index position 0 (i.e. first), and the original index 0 Adam students, and all students behind, automatically move one back.


Task:
assuming that a new student Paul, Paul students score better than Bart, but worse than the Lisa, he should be discharged to the third place, implement in code.

From writing code:

L = ['Adam', 'Lisa', 'Bart']
L.insert(2,'Paul')
print L

Here Insert Picture Description

5, remove elements from the list

Paul had just arrived a few days the students have to turn away, then how do we put Paul deleted from the existing list in it?

If Paul students came in last, we can use list of pop () method to delete:

>>> L = ['Adam', 'Lisa', 'Bart', 'Paul']
>>> L.pop()
'Paul'
>>> print L
['Adam', 'Lisa', 'Bart']

After the pop () method always delete the last element of the list, and it returns this element, so we execute L.pop (), prints out 'Paul'.

If Paul is not ranked in the students how to do the last one? For example, Paul ranked third classmate:

>>> L = ['Adam', 'Lisa', 'Paul', 'Bart']

Paul put kicked list, we must first locate the position of Paul. Because of Paul's index is 2, therefore, use pop (2) to Paul deleted:

>>> L.pop(2)
'Paul'
>>> print L
['Adam', 'Lisa', 'Bart']

Task:
Note to the right of the editor code list is as follows:

L = [‘Adam’, ‘Lisa’, ‘Paul’, ‘Bart’]

Paul's index is 2, Bart index is 3, if we want Paul and Bart are deleted, please explain why the following code does not run correctly:

L.pop(2)
L.pop(3)

How to adjust the code can be put Paul and Bart are properly removed?




From writing code:

L = ['Adam', 'Lisa', 'Paul', 'Bart']
L.pop(2)
L.pop(2)
print L

Here Insert Picture Description

6, replaced elements

Suppose now that the class is still three students:

>>> L = ['Adam', 'Lisa', 'Bart']

Now, Bart students to be transferred to another school, happened to be a classmate Paul, to update the list of members of the class, we can put Bart deleted, then Paul added.

Another way is to directly replace Paul to Bart:

>>> L[2] = 'Paul'
>>> print L
L = ['Adam', 'Lisa', 'Paul']

For one assignment list in the index, you can directly replace with new elements out of the original elements, the number of elements included in the list remain unchanged.

Since Bart -1 can also be used to index, therefore, the following code can accomplish the same replacement work:

>>> L[-1] = 'Paul'


Task:
Classmates ranked according to score like this:

L = [‘Adam’, ‘Lisa’, ‘Bart’]

However, after an examination, Bart accidentally get students first, and Adam students took the test last.

Please index assignment list to generate new ranking.

From writing code:

L = ['Adam', 'Lisa', 'Bart']
L[0]='Bart'
L[-1]='Adam'
print L

Here Insert Picture Description

Published 20 original articles · won praise 0 · Views 417

Guess you like

Origin blog.csdn.net/yipyuenkay/article/details/103873562