人妖在线一区,国产日韩欧美一区二区综合在线,国产啪精品视频网站免费,欧美内射深插日本少妇

新聞動(dòng)態(tài)

Python字典中items()函數(shù)案例詳解

發(fā)布日期:2022-01-25 16:26 | 文章來(lái)源:CSDN

Python3:字典中的items()函數(shù)

一、Python2.x中items():

  和之前一樣,本渣渣先貼出來(lái)python中help的幫助信息:

>>> help(dict.items)
Help on method_descriptor:
items(...)
 D.items() -> list of D's (key, value) pairs, as 2-tuples
>>> help(dict.iteritems)
Help on method_descriptor:
iteritems(...)
 D.iteritems() -> an iterator over the (key, value) items of D
>>> help(dict.viewitems)
Help on method_descriptor:
viewitems(...)
 D.viewitems() -> a set-like object providing a view on D's items

       在Python2.x中,items( )用于 返回一個(gè)字典的拷貝列表【Returns a copy of the list of all items (key/value pairs) in D】,占額外的內(nèi)存。

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

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

二、Python3.x中items():

>>> help(dict.items)
Help on method_descriptor:
items(...)
 D.items() -> a set-like object providing a view on D's items

  Python 3.x 里面,iteritems() 和 viewitems() 這兩個(gè)方法都已經(jīng)廢除了,而 items() 得到的結(jié)果是和 2.x 里面 viewitems() 一致的。在3.x 里 用 items()替換iteritems() ,可以用于 for 來(lái)循環(huán)遍歷。

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

簡(jiǎn)單的例子:

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))

輸出結(jié)果:

D:\Users\WordZzzz\Desktop>python3 test.py

Adam : 95

Lisa : 85

Bart : 59

Paul : 74

平均分為:78.25

到此這篇關(guān)于Python字典中items()函數(shù)案例詳解的文章就介紹到這了,更多相關(guān)Python字典中items()函數(shù)內(nèi)容請(qǐng)搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!

版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。

相關(guān)文章

實(shí)時(shí)開(kāi)通

自選配置、實(shí)時(shí)開(kāi)通

免備案

全球線(xiàn)路精選!

全天候客戶(hù)服務(wù)

7x24全年不間斷在線(xiàn)

專(zhuān)屬顧問(wèn)服務(wù)

1對(duì)1客戶(hù)咨詢(xún)顧問(wèn)

在線(xiàn)
客服

在線(xiàn)客服:7*24小時(shí)在線(xiàn)

客服
熱線(xiàn)

400-630-3752
7*24小時(shí)客服服務(wù)熱線(xiàn)

關(guān)注
微信

關(guān)注官方微信
頂部