python中tuple/list/dict/set常用方法
来源:互联网 发布:石油来源 知乎 编辑:程序博客网 时间:2024/06/05 19:12
一 元组
# 1 定义tup1 = () # 空tup2 = (0, ) # 注意,必须有分号tup3 = (1, 2, 3, 4)# 2 操作# 2.1 合并tup4 = tup2 + tup3 # (0, 1, 2, 3, 4)# 2.2 长度print len(tup4) # 5# 2.3 切片print tup4[0:4:2] # (0, 2)# 3 遍历for val in tup4:print val'''01234'''for i in range(len(tup4)):print i, tup4[i]'''0 01 12 23 34 4'''
二 列表
# 1 定义list1 = [] # 空list2 = [0]list3 = [1, 2, 3, 4]# 2 操作# 2.1 合并list4 = list2 + list3 # [0, 1, 2, 3, 4]# 2.2 长度print len(list4) # 5# 2.3 切片print list4[0:4:2] # [0, 2]# 2.4 追加list2.append(5) # [0, 5]list2.append([6, 7]) # [0, 5, 6, 7]# 2.5 移出list2.pop(0) # [5], 不指定下标默认移出最后一项# 3 遍历for val in list4:print val'''01234'''for i in range(len(list4)):print i, list4[i]'''0 01 12 23 34 4'''for i, item in enumerate(list4):print i , item'''0 01 12 23 34 4'''
三 字典
# 1 定义dic1 = {} # 空dic2 = {'one': 1}dic3 = {'two': 2}# 2 操作# 2.1 合并dic3.update(dic2) # {'one': 1, 'two': 2}# 或者# dic3 = dict(dic3, **dic2)# 2.2 长度print len(dic3) # 3# 2.3 keysprint dic3.keys() # ['one', 'two']# 2.4 valuesprint dic3.values() #[1, 2]# 2.3 itemsprint dic3.items() # [('one', 1), ('two', 2)]# 2.4 inprint 'one' in dic3 # Trueprint 1 in dic3 # False# 2.5 popdic3.pop('one') # {'two': 2}# 3 遍历for val in dic3: # for val in dic3.iterkeys():print val'''onetwo'''for val in dic3.itervalues():print val'''12'''for val in dic3.iteritems():print val'''('one', 1)('two', 2)'''for key, val in dic3.iteritems():print key, val'''one 1two 2'''
四 集合
# 1 定义set1 = set()set2 = set([0,1]) #set([0, 1])print set2# 2 操作# 2.1 长度print len(set2) # 2# 2.2 添加set2.add(2) # 一项 ([0, 1, 2])set2.update([3, 4]) # 多项 ([0, 1, 2, 3, 4])# 2.3 移出set2.remove(4) # ([0, 1, 2, 3])# 2.4 集合运算p1 = set([1, 2])p2 = set([2, 3])print p1 & p2 # 交集'''set([2])'''print p1 - p2 # 差集'''set([1])'''print p1 & p2 # 差集'''set([3])'''print p1 | p2 # 并集'''set([1, 2, 3])'''# 3 遍历for val in set2:print val'''0123'''
0 0
- python中tuple/list/dict/set常用方法
- Python中list、tuple、dict、set总结
- python list tuple dict set
- Python--list,tuple,dict,set.
- python学习系列---list,tuple,dict,set内置方法
- Python中set、list、dict、tuple的比较
- Python中list/tuple/dict/set学习总结
- python中list、set、tuple、dict比较分析
- python中list,tuple,dict,set的区别及联系
- python中 list和tuple 、dict和set区别
- Python中list,tuple,dict,set的区别和用法
- Python中list,tuple,dict,set的区别和用法
- python中list/tuple/dict/set的区别
- Python中list,tuple,dict,set的区别和用法
- Python - list, tuple, dict, set的区别
- Python set list dict tuple 区别
- Python List+Tuple+Dict+Set小结
- python之list,tuple,set,dict
- Nodejs学习笔记之从bodyParse来看app.use()
- Windows平台下CodeBlocks+Wxwidgets开发环境搭建
- 如何手工释放linux内存
- hook模板x86/x64通用版(2)--中转函数的shellcode编写
- 浏览器-页签显示图片
- python中tuple/list/dict/set常用方法
- MoveWindow的问题
- 算法导论22章基本的图算法 思考题总结
- mytop安装,使用mytop监控MySQL性
- iOS与JS交互实战篇(ObjC版)
- Java基础整理-Java试题(一)
- iOS 关闭键盘 退出键盘 的5种方式
- 竞争条件和关键区
- Itext5.8.8 Html 转化成PDF