list,tuple和dict详细方法
在python交互行
增:append,insert,extend
>>> b=['a','b','c'] >>> b.append('d') >>> b ['a', 'b', 'c', 'd'] >>> b.insert(1,'z') >>> b ['a', 'z', 'b', 'c', 'd'] >>> a=[1,2,3] >>> b.extend(a) >>> b ['a', 'z', 'b', 'c', 'd', 1, 2, 3]
删:pop,remove ,clear (非列表自带方法del)
>>> b ['a', 'b', 'c', 1, 2, 3] >>> b.pop() #默认最后 3 >>> b ['a', 'b', 'c', 1, 2] >>> b.pop(3) 1 >>> b ['a', 'b', 'c', 2] >>> b.remove('b') >>> b ['a', 'c', 2] >>> del b[0] >>> b ['c', 2]
改:切片
>>> b ['a', 'b', 'c', 1, 2, 3] >>> b[5]='f' >>> b ['a', 'b', 'c', 1, 2, 'f'] >>> b[3:5]=['d','e'] >>> b ['a', 'b', 'c', 'd', 'e', 'f']
查:切片,count,index
>>> b=['a', 'b', 'c', 'd', 'e', 'f'] >>> b[1:5:2] ['b', 'd'] >>> b[-6:-1:2] ['a', 'c', 'e'] >>> b[-1:-6:-2] ['f', 'd', 'b'] >>> b[-1:1:-2] ['f', 'd'] >>> b.count('a') #计算元素要数 1 >>> 'b' in b True >>> b.index('c') #查看下标即索引位置 2
排序 sort reverse (sorted非列表自带方法)
>>> a=[1,3,4,2,3,4,6,1,5,2] >>> a.sort() >>> a [1, 1, 2, 2, 3, 3, 4, 4, 5, 6] >>> a.reverse() >>> a [6, 5, 4, 4, 3, 3, 2, 2, 1, 1] >>> a=[1,3,4,2,3,4,6,1,5,2] >>> a.sort(reverse=True) >>> a [6, 5, 4, 4, 3, 3, 2, 2, 1, 1]
a=[1,3,4,2,3,4,6,1,5,2]
sorted(a,reverse=True)
[6, 5, 4, 4, 3, 3, 2, 2, 1, 1]
其他非内置方法
1、cmp(list1, list2):比较两个列表的元素
2、len(list):列表元素个数
3、max(list):返回列表元素最大值
4、min(list):返回列表元素最小值
5、list(seq):将元组转换为列表
tuple(元组)不可变,所以代码更安全。如果可能,能用tuple代替list就尽量用tuple。
定义一个元素需逗号分开 name=('a',)
tuple的每个元素,指向永远不变,指向了列表,列表元素可变
dict 创建
方法一:传统的文字表达式 >>> dict1={'name':'li','age':28,'work':'it'} 方法二:动态分配键值 >>> dict2={} >>> dict2['name']='li' >>> dict2['age']=28 >>> dict2['work']='it' >>> dict2 {'age': 28, 'work': 'it', 'name': 'li'} 方法三:字典键值表 >>> dict3=dict(name='li',age=28,work='it') >>> dict3 {'age': 28, 'work': 'it', 'name': 'li'} 方法四:字典键值元组表 >>> dict4=dict([('name','li'),('age',28),('work','it')]) >>> dict4 {'age': 28, 'work': 'it', 'name': 'li'} 方法五:所有键的值都相同或者赋予初始值 >>> dict5=dict.fromkeys(['name','age','work'],'null') >>> dict5 {'age': 'null', 'work': 'null', 'name': 'null'}
增: 赋值,setdefault
方法1 >>> dict1={'name':'li','age':28,'work':'it'} >>> dict1['sex']='man' #如果存在就会修改 >>> dict1 {'age': 28, 'work': 'it', 'name': 'li', 'sex': 'man'} 方法2 >>>dict1={'name':'li','age':28,'work':'it'} >>>dict1.setdefault('sex','man') #如果键存在不会更新,还是之前值 'man' >>>dict1 {'name': 'li', 'age': 28, 'work': 'it', 'sex': 'man'}
删:字典方法pop ,clear (非字典方法del)
>>> dict1={'name': 'li', 'age': 28, 'work': 'it', 'sex': 'man'} >>> del dict1['sex'] >>> dict1 {'age': 28, 'work': 'it', 'name': 'li'} >>> dict1.pop('work') #默认随机删除 'it' >>> dict1 {'age': 28, 'name': 'li'} >>> dict1.clear() >>> dict1 {}
改 赋值,update
方法一 赋值 >>> dict1={'name':'li','age':28,'work':'it'} >>> dict1['name']='huang' >>> dict1 {'age': 28, 'work': 'it', 'name': 'huang'} 方法二:update >>> dict1={'name':'li','age':28,'work':'it'} >>> dict1.update({'name':'hu','age':10}) >>> dict1 {'age': 10, 'work': 'it', 'name': 'hu'}
查 : 查键值,get,keys,items,values,in
>>> dict1={'name':'li','age':28,'work':'it'} >>> dict1['name'] 'li' >>> dict1.get('name') 'li' >>> dict1.keys() ['age', 'work', 'name'] >>> dict1.values() [28, 'it', 'li'] >>> dict1.items() [('age', 28), ('work', 'it'), ('name', 'li')] >>> 'name' in dict1 True
排序:非字典自带sorted reverse
>>>dict1={'name':'li','age':28,'work':'it'} >>> sorted(dict1) ['age', 'name', 'work'] >>> sorted(dict1.keys()) ['age', 'name', 'work'] >>> sorted(dict1.items()) [('age', 28), ('name', 'li'), ('work', 'it')] >>> sorted(dict1.values()) [28, 'it', 'li'] >>> sorted(dict1.values(),reverse=True) ['li', 'it', 28]
坚持到无能为力,拼搏到感动自己