python list,tuple,set,dict

--------------------------------------------------------
list 可变
1.创建
lista = ['a','b','c','d']
listb = list('abc')
2.修改
lista[2] = 5
3.增加
lista + listb
lista.append(listb)
lista.extend(listb)
lista.insert(2,listb)
4.删除
lista.pop() 弹出最后一个
lista.pop(2) 弹出第三个
lista.remove('b') 移除元素b
del lista 删除引用
del lista[:] 清空列表  lista.clear()

del lista[0] 删除第一个
5.查询
lista[2] 查询第三个元素
切片
in 和 not in
6.其他
lista.sort()
lista.reverse()
--------------------------------------------------------
tuple 不可变 与字符串类似
1.创建
tuplea = (1,2,3)
tupleb = tuple('abc')
2.修改
3.增加
tuplec = tuplea + tupleb
4.删除
del tuplea 删除元组的引用
5.查询
tuplea[2] 查询第三个元素
切片
in 和 not in
--------------------------------------------------------
set 无序不重复 (frozenset不可变)
1.创建
seta = {'a','b','c'}
setb = set('abc')
setc = set(['a','b','c'])
2.修改
3.增加
seta.add('python') 将python加入到集合
seta.update('python') 将'p','y','t','h','o','n' 加入到集合
4.删除
seta.remove('a') 将元素a 删除
del seta 删除set的引用
5.查询
不支持索引
in 和 not in
6.其他
交集并集差集&|-
--------------------------------------------------------
dict 无序,key不重复
1.创建
dicta ={'name':'lis','age':20}
dictb = dict(name ='zhangs',age=28)
2.修改
dicta['name'] = 'wangwu'
3.增加
dicta['city'] = 'wuhan'
dicta.update({'shenggao':180,'tizhong':68})
4.删除
del dicta['city'] 删除指定元素
dicta.clear() 删除字典全部元素
dicta.pop('city') 弹出指定元素
dicta.pop('cityssss','haha') 弹出不存在元素 输出haha
5.查询
dicta['name']
dicta.has_key('city') 返回True
dicta.keys() 返回所有key list
dicta.values() 返回所有value list
dicta.items() 返回字典容器[()] 外面list,里面tuple
dicta.get('name') 返回指定key的值
dicta.get('name222','haha') 不存在返回haha

-----------------------------------------------------------

posted @ 2017-04-19 23:01  1916  阅读(214)  评论(0)    收藏  举报