欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python3實(shí)現(xiàn)的字典遍歷操作詳解

 更新時間:2018年04月18日 14:49:31   作者:快遞小可  
這篇文章主要介紹了Python3實(shí)現(xiàn)的字典遍歷操作,結(jié)合實(shí)例形式分析了Python3針對字典鍵、鍵值及鍵值對遍歷的相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了Python3字典遍歷操作。分享給大家供大家參考,具體如下:

字典是針對非序列集合而提供的一種數(shù)據(jù)類型。

通過任意鍵查找集合中值信息的過程叫映射,python通過字典實(shí)現(xiàn)映射。

為字典賦值:

>>> d={'list':[1, 2, 3],1:123,'111':'python3','tuple':(4, 5, 6)}
>>> print(d)
{1: 123, 'list': [1, 2, 3], '111': 'python3', 'tuple': (4, 5, 6)}

以上語句說明,字典中各項(xiàng)的順序與賦值時的順序可能不一致,即字典是無序的。

字典的遍歷有一下幾種:

1. 遍歷字典的鍵key

>>> d={'list':[1, 2, 3],1:123,'111':'python3','tuple':(4, 5, 6)}
>>> for key in d:
    print(str(key)+':'+str(d[key]))
list:[1, 2, 3]
1:123
111:python3
tuple:(4, 5, 6)

>>> d={'list':[1, 2, 3],1:123,'111':'python3','tuple':(4, 5, 6)}
>>> for key in d.keys():
    print(key)
1
list
111
tuple

2. 遍歷字典的值value

>>> d={'list':[1, 2, 3],1:123,'111':'python3','tuple':(4, 5, 6)}
>>> for value in d.values():
    print (value)
[1, 2, 3]
123
python3
(4, 5, 6)

3. 遍歷字典的項(xiàng)

>>> d={'list':[1, 2, 3],1:123,'111':'python3','tuple':(4, 5, 6)}
>>> for item in d.items():
    print(item)
('list', [1, 2, 3])
(1, 123)
('111', 'python3')
('tuple', (4, 5, 6))

4. 遍歷字典的key-value

>>> d={'list':[1, 2, 3],1:123,'111':'python3','tuple':(4, 5, 6)}
>>> for key,value in d.items():
    print(key,value)
list [1, 2, 3]
1 123
111 python3
tuple (4, 5, 6)

>>> d={'list':[1, 2, 3],1:123,'111':'python3','tuple':(4, 5, 6)}
>>> for (key,value) in d.items():
    print(key,value)
list [1, 2, 3]
1 123
111 python3
tuple (4, 5, 6)

上述示例運(yùn)行效果如下圖所示:

以上便是,python字典遍歷的幾種方式。

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python字典操作技巧匯總》、《Python列表(list)操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python編碼操作技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進(jìn)階經(jīng)典教程

希望本文所述對大家Python程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評論