Learn python with socratica [My notes] - part 12- Lists in Python

Lesson 14

List[序列]是Python中最基本的数据结构。序列中的每个元素都分配一个数字,即它的位置,或着称为索引,第一个索引是0,第二个索引是1,依此类推。本节讲述如何创建一个List,并且规范化这个过程。

有两种创建List的方式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# One way is to use the list structure
example = list()
example = []

# 当创建一个序列的时候,可以给它初始化一些值
primes = [2,3,5,7,11,13]

# 你可以使用append方法对该序列添加成员
# 添加 17,19
primes.append(17)
primes.append(19)

# display the list and you will see it contains new values.
>>> primes
> [2, 3, 5, 7, 11, 13, 17, 19]

此时,你会发现,list的成员顺序与set是不一样的。在set中,顺序并不重要,但是在list中,顺序是一切。想要得到list中的一个成员,你只要知道它的索引,而不需要把所有成员遍历一次。在计算机中,索引从0开始,而不是1,我们将上面生成的list做个索引的对应。

比如,我们想得到第一项,该怎么做?

1
2
# type the name of the list and the index in brackets
primes[0]
2

这是按照从左到右的顺序展开,当然你会想到这种情况,如果你输入-1呢?是第一个成员左边的什么神秘数呢?

1
primes[-1]
19

结果是list的最后一项,并不是所想的什么神秘数,也就是python把序列的最后一项提到了第一项的左边。以此类推,倒数第二项就是-2。

1
primes[8]
---------------------------------------------------------------------------

IndexError                                Traceback (most recent call last)

<ipython-input-4-71a05b4dcdbd> in <module>()
----> 1 primes[8]

IndexError: list index out of range
1
primes[-9]
---------------------------------------------------------------------------

IndexError                                Traceback (most recent call last)

<ipython-input-5-b35b831943c2> in <module>()
----> 1 primes[-9]

IndexError: list index out of range

不管用哪种顺序,越界都会报错。

另一种得到序列的方式是切片[slicing]:

1
2
3
# retrieve a range of values from your list
# structure: name of list [a starting index : a stopping index]
primes[2:5]
[5, 7, 11]

注意一下,slicing包含起点,不包含终止点,再比如slicing[0:6]就不包括17。

1
primes[0:6]
[2, 3, 5, 7, 11, 13]

还有一点和set是一样的,list也可以包含所有的数据类型,并且可以包含子序列。这是python相较于其他编程语言比较方面的地方。

1
example = [128,True,1.23,'hello',[64,False]]

list还可以包含duplicate value【重复的值】,这是与set的第二点不同。

1
2
3
4
5
6
rolls = [4,7,2,7,12,4,7] # no error

# you can combine two list into a single list - using concatenation
numbers = [1,2,3]
letters = ['a','b','c']
numbers + letters
[1, 2, 3, 'a', 'b', 'c']
1
2
# order is everything
letters + numbers
['a', 'b', 'c', 1, 2, 3]

对序列的操作有很多,一样的,用dir(),help()去查询:

1
dir(numbers)
['__add__',
 '__class__',
 '__contains__',
 '__delattr__',
 '__delitem__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__getitem__',
 '__gt__',
 '__hash__',
 '__iadd__',
 '__imul__',
 '__init__',
 '__init_subclass__',
 '__iter__',
 '__le__',
 '__len__',
 '__lt__',
 '__mul__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__reversed__',
 '__rmul__',
 '__setattr__',
 '__setitem__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 'append',
 'clear',
 'copy',
 'count',
 'extend',
 'index',
 'insert',
 'pop',
 'remove',
 'reverse',
 'sort']
1
2
# for example, reverse function
help(numbers.reverse) # 反转
Help on built-in function reverse:

reverse(...) method of builtins.list instance
    L.reverse() -- reverse *IN PLACE*

总结一下,list的索引从0开始,当序列创建好了,你可以对它进行串联、添加、反转、清空等操作,更详细的List教程就在python的文档中了,用好dir()和help()。

Youtube source:
https://www.youtube.com/watch?v=bY6m6_IIN94&list=PLi01XoE8jYohWFPpC17Z-wWhPOSuh8Er-