Pyhton学习 dict.items()函数(一)

Python3:字典中的items()函数



一、Python2.x中items():

  和之前一样,本渣渣先贴出来python中help的帮助信息:

>>> help(dict.items)
Help on method_descriptor:

items(...)
    D.items() -> list of D's (key, value) pairs, as 2-tuples
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
>>> help(dict.iteritems)
Help on method_descriptor:

iteritems(...)
    D.iteritems() -> an iterator over the (key, value) items of D
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
>>> help(dict.viewitems)
Help on method_descriptor:

viewitems(...)
    D.viewitems() -> a set-like object providing a view on D's items
 
 
  • 1
  • 2
  • 3
  • 4
  • 5

  在Python2.x中,items( )用于 返回一个字典的拷贝列表【Returns a copy of the list of all items (key/value pairs) in D】,占额外的内存。

  iteritems() 用于返回本身字典列表操作后的迭代【Returns an iterator on all items(key/value pairs) in D】,不占用额外的内存。

>>> d={1:'one',2:'two',3:'three'}
>>> type(d.items())
<type 'list'>
>>> type(d.iteritems())
<type 'dictionary-itemiterator'>
>>> type(d.viewitems())
<type 'dict_items'>
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

二、Python3.x中items():

>>> help(dict.items)
Help on method_descriptor:

items(...)
    D.items() -> a set-like object providing a view on D's items
 
 
  • 1
  • 2
  • 3
  • 4
  • 5

  Python 3.x 里面,iteritems() 和 viewitems() 这两个方法都已经废除了,而 items() 得到的结果是和 2.x 里面 viewitems() 一致的。在3.x 里 用 items()替换iteritems() ,可以用于 for 来循环遍历。

>>> d={1:'one',2:'two',3:'three'}
>>> type(d.items())
<class 'dict_items'>
 
 
  • 1
  • 2
  • 3

简单的例子:

d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59, 'Paul': 74 }
sum = 0
for key, value in d.items():
    sum = sum + value
    print(key, ':' ,value)
print('平均分为:' ,sum /len(d))
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

输出结果:

D:\Users\WordZzzz\Desktop>python3 test.py
Adam : 95
Lisa : 85
Bart : 59
Paul : 74
平均分为: 78.25
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

关于python3.x中items具体的应用,可以通过下面的传送门到达机器学习实战中找到:

系列教程持续发布中,欢迎订阅、关注、收藏、评论、点赞哦~~( ̄▽ ̄~)~

完的汪(∪。∪)。。。zzz

				<script>
					(function(){
						function setArticleH(btnReadmore,posi){
							var winH = $(window).height();
							var articleBox = $("div.article_content");
							var artH = articleBox.height();
							if(artH > winH*posi){
								articleBox.css({
									'height':winH*posi+'px',
									'overflow':'hidden'
								})
								btnReadmore.click(function(){
									articleBox.removeAttr("style");
									$(this).parent().remove();
								})
							}else{
								btnReadmore.parent().remove();
							}
						}
						var btnReadmore = $("#btn-readmore");
						if(btnReadmore.length>0){
							if(currentUserName){
								setArticleH(btnReadmore,3);
							}else{
								setArticleH(btnReadmore,1.2);
							}
						}
					})()
				</script>
				</article>
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值