企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持知识库和私有化部署方案 广告
[TOC] ## 四种数据结构 * 列表 * 元组 * 集合 * 字典 ## 四种内置数据结构 ![](https://img.kancloud.cn/1d/4e/1d4e87e4e97d387e76ccb5085710b349_768x579.png) ## 列表 ~~~ 新建列表 a=[] 增加元素 .append 可以向列表添加元素 .extend 可以将另一个列表的元素添加到列表中。 .insert 在指定位置index前插入元素object。 查找元素 in (存在),如果存在那么结果为true,否则为false。 not in (不存在),如果不存在那么结果为true,否则false 修改元素 pop: 根据id删除元素(从最后计算),并返回删除的值 remove: 根据元素的值进行删除 排序 sort方法: 列表的元素按照特定顺序排列 reverse方法:将列表逆置 ~~~ ## 元组 新建元组 ~~~ a = () ~~~ 统计元组的个数 ~~~ count ~~~ 访问元组 ~~~ tuple=('hello',100,4.5) print(tuple\[0\]) print(tuple\[1\]) print(tuple\[2\]) ~~~ 元组内置函数 ~~~ len(tuple) 计算元组元素个数 max(tuple) 返回元组中元素最大值 min(tuple) 返回元组中元素最小值 tuple(seq) 将列表转为元组 ~~~ ## 字典 * 新建字典 ~~~ a = {'键':'值'} ~~~ * get方法 如果我们不确定字典中是否存在某个键而又想获取其值时,可以使用get方法,还可以设置默认值 * 获取所有的健 dict1.keys() ``` print(dict1.keys()) for key in dict1.keys(): print(key) ``` * 获取所有的值 dict1.values ``` print(dict1.values()) for key in dict1.values(): print(key) ``` * 获取字典的所有项 ``` print(dict1.items()) for key in dict1.items(): print(key[0],'----',key[-1]) ``` * 删除字典元素 ``` pop:根据key值删除字典重点的元素 clear:只是清空字典中的数据,字典还存在,只不过没有元素 ``` ## 集合 新建集合 ``` a = {} ``` 集合增删 ``` .add() 增 .remove() 默认-1,删掉最后一个 .clear() 清空所有 ``` union并集 ``` domain1 = {'www.baidu.com','wenke.baidu.com','www.baidu.com','zhidao.baidu.com'} domain2 = {'www.baidu.com','wenke.baidu.com','tieba.baidu.com'} print(domain1.union(domain2)) ```