---
title: "Python 3.3 教程 - 5 Python中的数据结构（6.2）"
author: "小聂飞"
date: "2013-03-25T10:43:23Z"
tags: []
categories: ["编程语言"]
slug: "python-3-3-5-python-6-2"
draft: false
lastmod: "2013-06-02T22:21:05Z"
id: 1828487
url: "https://teclusky.iteye.com/blog/1828487"
---

这一节将对你所学到的东西进行更深入的讲解。

 

5.1 关于List的更多介绍

 

list数据结构有很多的方法，以下是对这些方法的一些描述：

 

`list.append`(*x*)

Add an item to the end of the list. Equivalent to `a[len(a):] = [x]`.

`list.extend`(*L*)

Extend the list by appending all the items in the given list. Equivalent to `a[len(a):] = L`.

`list.insert`(*i*, *x*)

Insert an item at a given position. The first argument is the index of the element before which to insert, so `a.insert(0, x)` inserts at the front of the list, and `a.insert(len(a), x)` is equivalent to `a.append(x)`.

`list.remove`(*x*)

Remove the first item from the list whose value is *x*. It is an error if there is no such item.

`list.pop`(\[*i*])

Remove the item at the given position in the list, and return it. If no index is specified, `a.pop()` removes and returns the last item in the list. (The square brackets around the *i* in the method signature denote that the parameter is optional, not that you should type square brackets at that position. You will see this notation frequently in the Python Library Reference.)

`list.index`(*x*)

Return the index in the list of the first item whose value is *x*. It is an error if there is no such item.

`list.count`(*x*)

Return the number of times *x* appears in the list.

`list.sort`()

Sort the items of the list in place.

`list.reverse`()

Reverse the elements of the list in place.

 

以下例子使用了list中的大多数方法：

 

 

```
>>> a = [66.25, 333, 333, 1, 1234.5]
>>> print(a.count(333), a.count(66.25), a.count('x'))
2 1 0
>>> a.insert(2, -1)
>>> a.append(333)
>>> a
[66.25, 333, -1, 333, 1, 1234.5, 333]
>>> a.index(333)
1
>>> a.remove(333)
>>> a
[66.25, -1, 333, 1, 1234.5, 333]
>>> a.reverse()
>>> a
[333, 1234.5, 1, 333, -1, 66.25]
>>> a.sort()
>>> a
[-1, 1, 66.25, 333, 333, 1234.5]
```

 

 

你或许已经发现了诸如insert, remove或者sort这类方法修改了list中的数据，而且它们不会返回任何可打印的值（意味着这些方法返回的是None）。注意，这个是在Python中针对可变数据结构的一种设计原则。

 

5.1.1 将List作为堆栈来使用

 

list中的方法使得其作为堆栈来使用是非常容易的，堆栈意味着最后一个添加的数据是第一个被取到的（last in, and first out）.为了添加一个数据到堆顶，可以使用append()方法。为了从堆顶取到一个数据，可以使用pop()方法（不需要指明所要取数据的Index）。比如：

 

 

```
>>> stack = [3, 4, 5]
>>> stack.append(6)
>>> stack.append(7)
>>> stack
[3, 4, 5, 6, 7]
>>> stack.pop()
7
>>> stack
[3, 4, 5, 6]
>>> stack.pop()
6
>>> stack.pop()
5
>>> stack
[3, 4]
```

 

 

5.1.2 将List作为队列来使用

 

将list作为队列来使用也是可以的，这意味着第一个添加的数据将会第一个被取到（fist in, and first out）。尽管如此，list作为队列来使用并不是很有效的。对于list来说，在结尾处添加和获取数据是非常快速的，而在list起始处进行数据获取或插入是非常耗费时间的（因为所有后面的数据需要进行移位）。-NF：可见list在Python中的实现应该使用的是数组这种数据结构。

 

为了更好的实现一个队列，可以使用collections.deque，这个结构在两端进行插入或者删除操作都会有很好的效率。比如：

 

 

```
>>> from collections import deque
>>> queue = deque(["Eric", "John", "Michael"])
>>> queue.append("Terry")           # Terry arrives
>>> queue.append("Graham")          # Graham arrives
>>> queue.popleft()                 # The first to arrive now leaves
'Eric'
>>> queue.popleft()                 # The second to arrive now leaves
'John'
>>> queue                           # Remaining queue in order of arrival
deque(['Michael', 'Terry', 'Graham'])
```

 

 

5.1.3 List解析

 

5.1.4 嵌套的List解析

 

5.2 del语句

 

5.3 元组和序列

 

5.4 集合

 

5.5 字典

 

5.6 循环技术

 

5.7 关于条件的更多介绍

 

5.8 序列和其它类型的比较