# python的数据结构

三大类：`序列、映射、集合`

## 序列：列表\[]

```python
'''
函数:
len(list)：列表元素个数
max(list)：返回列表元素最大值
min(list)：返回列表元素最小值
list(seq)：将元组转换为列表

方法:
list.append(obj)：在列表末尾添加新的对象
list.count(obj)：统计某个元素在列表中出现的次数
list.extend(seq)：在列表末尾一次性追加另一个序列中的多个值（用新列表扩展原来的列表）
list.index(obj)：从列表中找出某个值第一个匹配项的索引位置
list.insert(index, obj)：将对象插入列表
list.pop(obj=list[-1])：移除列表中的一个元素（默认最后一个元素），并且返回该元素的值
list.remove(obj)：移除列表中某个值的第一个匹配项
list.reverse()：反向列表中元素
list.sort([func])：对原列表进行排序


copy()与deepcopy():
不可变的对象(数字，字符串，元组)，深浅拷贝没有区别

b=a
b=a.copy()

'''
```

## 序列：元组()

```python
'''
函数：
len(list)：元组元素个数
max(list)：返回元组元素最大值
min(list)：返回元组元素最小值
tuple(seq)：将列表转换为元组

'''

tup1 = (12, 34.56)
tup2 = ('abc', 'xyz')
# 修改元组是非法的
# tup1[0] = 100

# 使用del语句来删除整个元组
del tup1
```

## 序列：字符串str

```python
'{},{} '.format(a,b)
```

## 映射：字典dict

| 函数                  | 说明                                |
| ------------------- | --------------------------------- |
| D                   | 字典对象                              |
| D.clear()           | 清空字典                              |
| `D.pop(key)`        | 移除键，同时返回此键所对应的值                   |
| `D.copy()`          | 返回字典D的副本,`只复制一层(浅拷贝)`             |
| `D.update(D2)`      | 将字典 D2 合并到D中，如果键相同，则此键的值取D2的值作为新值 |
| D.get(key, default) | 返回键key所对应的值,如果没有此键，则返回default     |
| D.keys()            | 返回可迭代的 dict\_keys 集合对象            |
| D.values()          | 返回可迭代的 dict\_values 值对象           |
| D.items()           | 返回可迭代的 dict\_items 对象             |
| copy.deepcopy       | 深拷贝拷贝，父级目录，子级目录全部拷贝               |

```python
dict1 = {'user': 'test', 'num': [1, 2, 3]} # 原字典
dict2 = dict1 # 直接赋值
dict3 = dict1.copy() # 浅拷贝，只深拷贝父级目录


import copy
dict4 = copy.deepcopy(dict1) # 深拷贝拷贝，父级目录，子级目录全部拷贝
```

## 集合：set

```python
strs=set(['jeff','wong','cnblogs'])
nums=set(range(10))
```

| set()         | 创建一个空的集合对象(不能用{}来创建空集合) |
| ------------- | ----------------------- |
| set(iterable) | 用可迭代对象创建一个新的集合对象        |

| **方法**                              | **意义**                                  |
| ----------------------------------- | --------------------------------------- |
| S.add(e)                            | 在集合中添加一个新的元素e；如果元素已经存在，则不添加             |
| S.remove(e)                         | 从集合中删除一个元素，如果元素不存在于集合中，则会产生一个KeyError错误 |
| S.discard(e)                        | 从集合S中移除一个元素e,在元素e不存在时什么都不做;             |
| S.clear()                           | 清空集合内的所有元素                              |
| `S.copy()`                          | 将集合进行一次`浅拷贝`                            |
| `S.pop()`                           | 从集合S中`删除一个随机元素`;如果此集合为空，则引发KeyError异常   |
| S.update(s2)                        | 用 S与s2得到的全集更新变量S                        |
| `S.difference(s2)`                  | 用`S - s2` 运算，返回`存在于在S中，但不在s2中`的所有元素的集合  |
| `S.difference_update(s2)`           | 等同于 `S = S - s2`                        |
| S.intersection(s2)                  | 等同于 S & s2                              |
| S.intersection\_update(s2)          | 等同于S = S & s2                           |
| `S.isdisjoint(s2)`                  | 如果S与s2交集为空返回True,非空则返回False             |
| `S.issubset(s2)`                    | 如果S与s2交集为非空返回True,空则返回False             |
| S.issuperset(...)                   | 如果S为s2的子集返回True,否则返回False               |
| S.symmetric\_difference(s2)         | 返回对称补集,等同于 S ^ s2                       |
| S.symmetric\_difference\_update(s2) | 用 S 与 s2 的对称补集更新 S                      |
| `S.union(s2)`                       | 生成 S 与 s2的全集                            |


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://im-qianuxn.gitbook.io/pytorch/ji-suan-ji/python/shu-ju-jie-gou.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
