Python中的元组,列表与字典
来源:互联网 发布:python中pipy 编辑:程序博客网 时间:2024/05/20 05:25
1. 元组(Tuple)
1. 特点
元组是一个不可变的序列,不能给元组的一个独立的元素赋值,形式如下
2. Code
t=1,2,3,4,5,5t = t,(6,)#一个元素的元组最后要加个','#去重print tf = set(t[0])print f#遍历for each in t: print eachfor each in t: for item in each: print item
Input/Output
((1, 2, 3, 4, 5, 5), (6,))set([1, 2, 3, 4, 5])(1, 2, 3, 4, 5, 5)(6,)1234556
2. 列表(List)
1. 特点
与元组不同的是列表支持修改列表的某个元素。形式如下
2. 常用的方法
append() : 把一个元素添加到列表的结尾。extend() : 通过添加指定列表的所有元素来扩充列表。insert(i,x) : 在指定位置插入x remove(x) : 删除列表中值为x 的第一个元素。如果没有这样的元素,就会返回一个错误。pop(x) : 从列表的指定位置删除元素,并将其返回。如果没有指定索引,a.pop() 返回最后一个元素。元素随即从链表中被删除。count(x) : 返回x 在链表中出现的次数。sort() : 对链表中的元素进行适当的排序。reverse() : 对列表进行旋转
3. Code
#append() :把一个元素添加到列表的结尾。LIST = [1,2,3,4,5]LIST.append(6)print LIST#extend() 通过添加指定列表的所有元素来扩充列表。LIST2 =[7,8,9]LIST.extend(LIST2)print LIST#insert(i,x)在指定位置插入xLIST.insert(0,0)print LIST#remove(x) :删除列表中值为x的第一个元素。如果没有这样的元素,就会返回一个错误。LIST.remove(0)print LIST#pop(x):从列表的指定位置删除元素,并将其返回。如果没有指定索引,a.pop()返回最后一个元素。元素随即从链表中被删除。LIST.pop(1)print LIST#count(x):返回x在链表中出现的次数。print LIST.count(1)#sort():对链表中的元素进行适当的排序。#reverse():对列表进行旋转LIST.reverse()print LIST
4. Input/Output
[1, 2, 3, 4, 5, 6][1, 2, 3, 4, 5, 6, 7, 8, 9][0, 1, 2, 3, 4, 5, 6, 7, 8, 9][1, 2, 3, 4, 5, 6, 7, 8, 9][1, 3, 4, 5, 6, 7, 8, 9]1[9, 8, 7, 6, 5, 4, 3, 1]
3. 字典(Dictionary)
1. 特点:
相当于C++中的map,由一个二元组
2. 常用方法
cmp(dict1,dict2) :比较两个字典元素。len(dict) :计算字典元素个数,即键的总数。str(dict) :输出字典可打印的字符串表示。type(variable) :返回输入的变量类型,如果变量是字典就返回字典类型。- 5.删除字典/条目
del dir ,del dir[1]
3. Code
DIC={1:'a',2:'b',3:'c',4:'d'}for each in DIC: print DIC[each]del DIC[1]for each in DIC: print DIC[each]
Input/Output
a b c db c d
1 0
- Python中的元组,列表与字典
- python中的字典、元组、列表
- Python中的字典,列表,元组笔记
- python中的列表、元组、字典
- Python:元组、列表、字典
- python字典,列表,元组
- python 元组,列表,字典
- python 字典,列表,元组
- python 列表/元组/字典
- python--列表 、元组、字典
- python 列表、元组、字典
- python 列表,元组,字典
- Python 列表 元组 字典
- python列表元组字典
- Python 的元组、列表、字典区别与联系
- Python 元组、列表、字典、文件
- python的元组、列表、字典
- Python 元组、列表、字典、文件
- PHP+Mysql+AJAX登录验证
- Hashtable和HashMap类有三个重要的不同之处
- 织梦DedeCms如何批量修改文章发布时间
- Android中Math取整的三个方法
- (NO.00002)iOS游戏精灵战争雏形(十)
- Python中的元组,列表与字典
- hdu 2284 Solve the puzzle, Save the world!(组合数)
- Apache配置——监听多个端口 对应多个不同的网站目录
- 操作系统中的页面置换算法
- hdu 2803 The MAX【贪心】
- 容斥基础
- linux shell用法(4)-- shell逻辑符号与c兼容方法
- android.content.res.Resources$NotFoundException: String resource ID #0x0异常
- 数据库小析