Skip to content

Commit

Permalink
modify
Browse files Browse the repository at this point in the history
  • Loading branch information
WuShichao committed Oct 5, 2018
1 parent f271453 commit 130216d
Showing 1 changed file with 114 additions and 1 deletion.
115 changes: 114 additions & 1 deletion 07.data_structures.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,28 @@ Python 中有四种内置的数据结构——_列表(List)、元组(Tuple

项目的列表应该用方括号括起来,这样 Python 才能理解到你正在指定一张列表。一旦你创建了一张列表,你可以添加、移除或搜索列表中的项目。既然我们可以添加或删除项目,我们会说列表是一种_可变的(Mutable)_数据类型,意即,这种类型是可以被改变的。

## 有关对象与类的快速介绍
### 列表的一些特点

* _成员对象灵活_
```python
a = [99 , "bottles of beer", ["on", "the", "wall"]]
```
* _运算符与字符串相同_
```python
a+b, a*3, a[0], a[-1], a[1:], len(a)
```
* _内容易于修改_
```python
>>>a[0] = 9
>>>a[1:2] = ["bottles", "of", "beer"]
>>>print(a)
[98, "bottles", "of", "beer", ["on", "the", "wall"]]
>>>del a[-1]
>>>print(a)
[98, "bottles", "of", "beer"]
```

### 有关对象与类的快速介绍

虽然到目前为止我经常推迟有关对象(Object)与类(Class)的讨论,但现在对它们进行稍许解释能够有助于你更好地理解列表。我们将在[后面的章节](https://github.com/WuShichao/a-byte-of-python-bnu/tree/4e7952bd0b5a028cd3149f9b9cff837f08531314/14.oop.md#oop)讨论有关它们的更多细节。

Expand Down Expand Up @@ -78,6 +99,49 @@ My shopping list is now ['banana', 'carrot', 'mango', 'rice']

如果你想了解列表对象定义的所有方法,可以通过 `help(list)` 来了解更多细节。

### 列表方法

下面我们再通过几个具体例子来学习列表的方法:

```python
>>> a = range(5) # [0,1,2,3,4]
>>> print(list(a))
>>> a.append(5) # [0,1,2,3,4,5]
>>> a.pop() # [0,1,2,3,4]
5
>>> a.insert(0, 42) # [42,0,1,2,3,4]
>>> a.pop(0) # [0,1,2,3,4]
42
>>> a.sort() # [0,1,2,3,4]
>>> a.reverse() # [4,3,2,1,0]
# 字符串排序使用字典序,逐位比较字母
```

### 列表函数

```python
# range() 生成数组列表
>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(range(2, 30, 5))
[2, 7, 12, 17, 22, 27]
# sum() 求和
# min() 返回列表中最小元素
# max() 返回列表中最大元素
```

### 列表排序

```python
mylist = ["b", "C", "A", "a"]
# method of list
mylist.sort()
mylist.sort(key=str.lower)
# general function
nlist = sorted(mylist)
nlist = sorted(mylist,reverse=True)
```

## 元组

元组(Tuple)用于将多个对象保存到一起。你可以将它们近似地看作列表,但是元组不能提供列表类能够提供给你的广泛的功能。元组的一大特征类似于字符串,它们是_不可变的_,也就是说,你不能编辑或更改元组。
Expand Down Expand Up @@ -207,6 +271,55 @@ Guido's address is [email protected]
>
> 如果你曾在你的函数中使用过关键词参数,那么你就已经使用过字典了!你只要这么想——你在定义函数时的参数列表时,就指定了相关的键值—值配对。当你在你的函数中访问某一变量时,它其实就是在访问字典中的某个键值。(在编译器设计的术语中,这叫作_符号表(Symbol Table)_
### 字典构造

```python
sub = {'zhao':1, 'li':2, 'qian':3}
print(sub)
sub = dict(zhao = 1, li = 2, qian = 3)
print(sub)
keys = ['zhao', 'li', 'qian', 'sun']
vals = [1, 2 ,3, 4]
sub = dict(zip(keys,vals))
print(sub)
```

### 字典方法

```python
# Keys, values, items:
d.keys() -> ["duck", "back"]
d.values() -> ["duik", "rug"]
d.items() -> [("duck","duik"), ("back","rug")]
# 存在性检验
d.has_key("duck") -> 1; d.has_key("spam") -> 0
# 键值类型均随意
{"name":"Guido", "age":43, ("hello","world"):1, 42:"yes", "flag":["red", "white", "blue"]}
```

### 字典遍历

```python
d = dict(a=12, b="abc",c=15)
print(d)
for item in d.items():
print(item)
for key in d:
print(key,d[key])
for value in d.values():
print(value)
```

### 字典排序

```python
disordered = {10: 'b', 3: 'a', 5: 'c'}
sorted_dict = {k: disordered[k] for k in sorted(disordered)}
print(sorted_dict)
sorted_dict = sorted([(v,k) for (k,v) in disordered.items()], reverse=True)
print(sorted_dict)
```

## 序列 {#sequence}

列表、元组和字符串可以看作序列(Sequence)的某种表现形式,可是究竟什么是序列,它又有什么特别之处?
Expand Down

0 comments on commit 130216d

Please sign in to comment.