python字典访问的三种方法
2015-12-27 19:52
561 查看
定义字典 dic = {'a':"hello",'b':"how"}
法一:
法二:
法三:
法二和法三对比:
items()返回的是列表对象,而iteritems()返回的是iterator对象。例如:
print dic.items() #[('a', 'hello'), ('c', 'you'), ('b', 'how')]
print dic.iteritems() #<dictionary-itemiterator object at 0x020E9A50>
深究:iteritor是迭代器的意思,一次返回一个数据项,直到没有为止
for i in dic.iteritems():
print i
结果:('a', 'hello')
原文:http://www.cnblogs.com/kaituorensheng/archive/2012/09/05/2672711.html
法一:
for key in dic: print key,dic[key] # a hello 输出中间有空格 print key + str(dic[key]) # ahello 输出中间没空格
法二:
for (k,v) in dic.items(): print "dic[%s]="%k,v #dic[a]= hello
法三:
for k,v in dic.iteritems(): print "dic[%s]="%k,v # dic[a]= hello
法二和法三对比:
items()返回的是列表对象,而iteritems()返回的是iterator对象。例如:
print dic.items() #[('a', 'hello'), ('c', 'you'), ('b', 'how')]
print dic.iteritems() #<dictionary-itemiterator object at 0x020E9A50>
深究:iteritor是迭代器的意思,一次返回一个数据项,直到没有为止
for i in dic.iteritems():
print i
结果:('a', 'hello')
原文:http://www.cnblogs.com/kaituorensheng/archive/2012/09/05/2672711.html
相关文章推荐
- python3.4 Windows 安装配置pip
- python基础(google map api)
- Mac os 安装 MySQL和MySQL-Python
- python与逻辑回归
- python 内建模块
- Python Access学习(一)
- 【python学习笔记】用正则表达式从含中文的网页中提取数据(含编码转换)
- 学习Python编程的11个资源
- 使用python实现HMM
- python学习——基础(四)
- python jieba分词学习
- python中的生成器
- Python爬虫初步
- python 正则表达式
- python中的迭代器
- 安装python,setuptools,get-pip.py,ipython构建python开发环境
- python 20151227
- 【python】Django1.9 静态文件配置
- Python学习笔记——特殊方法
- python 自学笔记 20151224