濮阳杆衣贸易有限公司

主頁 > 知識(shí)庫 > Python字典中items()函數(shù)案例詳解

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

熱門標(biāo)簽:地圖標(biāo)注多個(gè) 萊蕪電信外呼系統(tǒng) 企業(yè)微信地圖標(biāo)注 鶴壁手機(jī)自動(dòng)外呼系統(tǒng)違法嗎 怎么辦理400客服電話 B52系統(tǒng)電梯外呼顯示E7 高德地圖標(biāo)注收入咋樣 銀川電話機(jī)器人電話 沈陽防封電銷電話卡

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

一、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
>>> 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 來循環(huán)遍歷。

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

簡單的例子:

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)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • Python中使用items()方法返回字典元素對(duì)的教程
  • Python中dictionary items()系列函數(shù)的用法實(shí)例
  • 淺談Python的字典鍵名可以是哪些類型
  • python字典的元素訪問實(shí)例詳解
  • python字典遍歷數(shù)據(jù)的具體做法
  • python用函數(shù)創(chuàng)造字典的實(shí)例講解

標(biāo)簽:湘西 葫蘆島 烏魯木齊 呼倫貝爾 安慶 三亞 銀川 呼倫貝爾

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Python字典中items()函數(shù)案例詳解》,本文關(guān)鍵詞  Python,字典,中,items,函數(shù),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Python字典中items()函數(shù)案例詳解》相關(guān)的同類信息!
  • 本頁收集關(guān)于Python字典中items()函數(shù)案例詳解的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    富顺县| 贵州省| 安溪县| 张掖市| 综艺| 收藏| 南溪县| 古田县| 广饶县| 石河子市| 林芝县| 股票| 保山市| 肃宁县| 望城县| 宜阳县| 马尔康县| 石楼县| 鲁山县| 汕头市| 沙坪坝区| 白银市| 新宾| 屯门区| 贵德县| 共和县| 波密县| 白银市| 富阳市| 德州市| 龙海市| 乌拉特前旗| 来宾市| 新巴尔虎左旗| 晋城| 西林县| 静安区| 子洲县| 达拉特旗| 彭泽县| 遂宁市|