最近图书馆借了本python,书不是自己的,还是自己写点笔记保险
========================================================
一 元组(tuple)
元组创建和不可修改性:
t0=()
# 创建一个空元组 t1 = ('a','b',1,1.5,'string') # 元素可以是字符、数字、字符串,甚至可以是其他元组、列表或字典等 print t0 print t1 print t1[2] # 输出结果是 () ('a', 'b', 1, 1.5, 'string') 1 ============================================================================ t1[0]='f' # 错误,因为元组创建后就不可修改 Traceback (most recent call last): File "<pyshell#9>", line 1, in <module> t1[0]='f' TypeError: 'tuple' object does not support item assignment ============================================================================ t1.append('e') # 错误,因为元组创建后就不可修改,也不可追加 Traceback (most recent call last): File "<pyshell#21>", line 1, in <module> t1.append('e') AttributeError: 'tuple' object has no attribute 'append' |
单元素元组的创建,必须在元素后面加一个逗号:
t2 = ('a',)
# 加逗号,创建的是单元素的元组 t3 = ('a') #不加逗号,创建的是个字符串 print t2 print t3 # 输出结果是 ('a',) a |
嵌套元组的创建和访问:
t4 = ('a','b') # 这里t4 是个元组,还可以是列表、字典,如t4=['a','b'] 或者t4={'a':'b'} t5 = (t4,'c','d') # 元组t5 的第一个元素是元组t4 print t4 print t5 print t5[0] # 访问元组t5 的第0个元素,即t4 print t5[0][1] # 访问元组t5 的第0个元素的第1个元素,即t4[1] # 输出结果是 ('a','b') (('a','b'),'c','d') ('a','b') b |
二 列表(list)
列表的创建和修改
# 列表创建 L0=[] # 创建空列表 L1=['a','b',1,1.5,'string'] # 元素可以是字符、数字、字符串,甚至可以是其他元组、列表或字典等 print t0 print t1 # 输出结果是 [] ['a', 'b', 1, 1.5, 'string'] ============================================================================ #列表修改 L1[0]='c' L1[2]=L1[2]+2 print L1 # 输出结果是: ['c', 'b', 3, 1.5, 'string'] ============================================================================ #列表追加 L0.append('hello') L0.append('word') print L0 #输出结果是: ['hello','word'] ---------------------------------------------------------------------------------------------------------------------------------- # 追加操作可以追加任何类型元素,字符、数字、字符串,甚至可以是其他元组、列表或字典等 L0.append(('a','b')) print L0 #输出结果是: ['hello','word',('a','b')] ---------------------------------------------------------------------------------------------------------------------------------- #但追加操作每次只能追加一个元素 L0.append('c','d') Traceback (most recent call last): File "<pyshell#33>", line 1, in <module> L0.append('a','b') TypeError: append() takes exactly one argument (2 given) ============================================================================ #列表扩展 # 列表扩展可以看成是一种"高级追加",可以一次性追加多个元素 e1 = ('c','d') # 用元组保存要扩展的元素 e2 = ['e','f','g'] # 用列表保存要扩展的元素 L0.extend(e1) print L0 L0.extend(e2) print L0 L0.extend(e2[2]) # 输出结果是: ['hello','word',('a','b'),'c','d'] ['hello','word',('a','b'),'c','d','e','f','g'] ['hello','word',('a','b'),'c','d','e','f','g','g'] |
从列表中弹出元素:
print L0 L0.pop() #默认弹出的是列表的最后一个元素 print L0 tt ==L0.pop(2) # 弹出L0[2],并返回L0[2]的值,pop()方法是唯一一个修改了列表又返回元素的值的列表方法 print tt print L0 # 输出结果是 ['hello','word',('a','b'),'c','d','e','f','g','g'] ['hello','word',('a','b'),'c','d','e','f','g'] ('a','b') ['hello','word','c','d','e','f','g'] |
三 字典(dict)
字典创建和修改
d0={} #创建空字典 d1={'a':97,'b':98} print d0 print d1 print d1[a] print d1[0] # 错误,字典访问元素不再按照默认的数字索引,而是按照字典键值访问,会报错keyError,因为d1中没有 0 这个键值 # 输出结果是 {} {'a':97,'b':98} 97 Traceback (most recent call last): File "<pyshell#48>", line 1, in <module> d1[0] KeyError: 0 ============================================================================ # 字典没有append() 和extend() 方法,字典可以随时添加元素 d1['c']=99 print d1 d1[97]='a' print d1 d1[98]='b' print d1 d1[99]='c' print d1 print d1[a] print d1[97] # 输出结果是 {'a':97,'b':98,'c':99} {'a':97,'b':98,'c':99,97:'a'} {'a':97,'b':98,'c':99,97:'a',98:'b'} {'a':97,'b':98,'c':99,97:'a',98:'b',99:'c'} 97 a ========================================= # 字典弹出元素 print d1 d1.pop(99) print d1 d1.pop() # 报错,字典的pop() 方法没有默认情况 # 输出结果是: {'a':97,'b':98,'c':99,97:'a',98:'b',99:'c'} {'a':97,'b':98,'c':99,97:'a',98:'b'} Traceback (most recent call last): File "<pyshell#57>", line 1, in <module> d1.pop() TypeError: pop expected at least 1 arguments, got 0 |
四 一些方法
# len() 方法 s='12345' print s print len(s) print t1 print len(t1) print L0 print len(L0) print d1 print len(d1) # 输出结果是 '12345' 5 ('a', 'b', 1, 1.5, 'string') 5 ['hello','word','c','d','e','f','g'] 7 {'a':97,'b':98,'c':99,97:'a',98:'b'} 5 |
用负值倒序引用元素:
print t1 print t1[-1] # t1的倒数第1个 print L0 print L0[-2] # L0的倒数第2个 # 输出结果是 ('a', 'b', 1, 1.5, 'string') string ['hello','word','c','d','e','f','g'] f |
切片操作:
print t1 print t1[2:] # t1的第2个元素开始到最后一个 print t1[2:4] # t1的第2,3个元素 print t1[:4] # 第4个之前的所有元素 print L0 print L0[-2:] # L0的倒数第2个到最后一个 print L0[:-2] # L0的倒数第2个之前的所有元素 # 输出结果是 ('a', 'b', 1, 1.5, 'string') ( 1, 1.5) ('a', 'b', 1, 1.5) ['hello','word','c','d','e','f','g'] ['f','g'] ['hello','word','c','d','e'] |
五 集合(set)
tt=('a','b','a','b') LL=['a','a','b','b','c','c'] set1=set(tt) set2=set(LL) print set1 print set2 # 输出结果是 set(['a','b']) set(['a','c','b']) |
从数据集中删除重复的数据时,集合非常有用