Python Crash Course study notes - Chapter 3: INTRODUCING LISTS

What is the List

List ordered a number of items (note not sorted) collection, with square brackets ( []representation), which items (also referred to as members) with a comma ( ,separately). List variable names are usually used in the plural.
List can have duplicate values.
List of members of different types can be mixed, but the examples in this chapter are the same type.
The project started by the index 0, and that the C language is the same.

>>> months = ['jan', 'feb', 'march', 'may']
>>> print(months)
['jan', 'feb', 'march', 'may']
>>> print(months[0] + ' ' + months[1])
jan feb
>>> print(months[0].title());
Jan

-1 may represent a last index, counting backwards from the right notation:

>>> print(months[-1])
may
>>> print(months[-2])
march

Additions and deletions to change members

>>> print (months)
['Jan', 'feb', 'march', 'may']
# 追加
>>> months.append('dec');
>>> print (months)
['Jan', 'feb', 'march', 'may', 'dec']
# 插入,3即index
>>> months.insert(3,'apr')
>>> print (months)
['Jan', 'feb', 'march', 'apr', 'may', 'dec']
# 修改
>>> months[-1]='DEC'
>>> print (months)
['Jan', 'feb', 'march', 'apr', 'may', 'DEC']
# 删除,很奇怪del居然不是method
>>> del months[-1]
>>> print (months)
['Jan', 'feb', 'march', 'apr', 'may']

There is also a special way of removing members, use pop () method:

# 默认删除最后一个成员,注意poped_month是单数
>>> poped_month = months.pop()
>>> print(months)
['Jan', 'feb', 'march', 'apr']
>>> print(poped_month)
may
# 也可pop指定的成员
>>> poped_month = months.pop(0)
>>> print(months)
['feb', 'march', 'apr']
>>> print(poped_month)
Jan

Value may also be deleted by a member (constants or variables can be specified), but remove () removes only the first matching values. :

>>> months.remove('march')
>>> print(months)
['feb', 'apr']

Clear List:

>>> months=[]
>>> print(months)
[]

Organization List

Sort by:

>>> months = [ 2, 5, 9, 1, 3, 10]
#正向排序
>>> months.sort()
>>> print(months)
[1, 2, 3, 5, 9, 10]

# 反向排序,注意True区分大小写,不能写成true
>>> months.sort(reverse=True)
>>> print(months)
[10, 9, 5, 3, 2, 1]

Sorted using a temporary sorting function, does not change the source List:

>>> months = [ 2, 5, 9, 1, 3, 10]
>>> print(sorted(months))
[1, 2, 3, 5, 9, 10]
>>> print(sorted(months, reverse=True))
[10, 9, 5, 3, 2, 1]
>>> print(months)
[2, 5, 9, 1, 3, 10]

Note sorted () is a function, the function has a return value, and therefore can be printed. And sort () is a method, no return value. Behind len () is also a function. The previous method will follow the object, and through the '' call.

Reverse the order:

>>> print(months)
[2, 5, 9, 1, 3, 10]
>>> months.reverse()
>>> print(months)
[10, 3, 1, 9, 5, 2]

Calculation of the number of members List:

>>> len(months)
6

Avoid mistakes Index

That is, to avoid index overrun:

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

The List is empty, the index is -1 will overrun.

Published 352 original articles · won praise 42 · views 550 000 +

Guess you like

Origin blog.csdn.net/stevensxiao/article/details/103973416