Python中字典遍歷的方法以及效能

2021-07-14 19:34:35 字數 3751 閱讀 9804

python中字典的遍歷方式有多種,』items』, 『iteritems』, 『keys』,』iterkeys』, 『itervalues』等方法都可以遍歷字典中的鍵或值,下面通過各種方法來看一下遍歷字典使用不同方法時的效能。**如下:

#!/usr/bin/env python

from time import clock

lst = [(x, x) for x in xrange(10000000)]

dic = dict(lst)

defmethod1

(): start_time = clock()

for i in dic :

t = i + dic[i]

end_time = clock()

print

"method 1 costs time is : " , str(end_time - start_time)

defmethod2

(): start_time = clock()

for k, v in dic.items() :

t = k + v

end_time = clock()

print

"method 2 costs time is : " , str(end_time - start_time)

defmethod3

(): start_time = clock()

for k, v in dic.iteritems() :

t = k + v

end_time = clock()

print

"method 3 costs time is : " , str(end_time - start_time)

defmethod4

(): start_time = clock()

for k in dic.keys():

t = k + dic[k]

end_time = clock()

print

"method 4 costs time is : " , str(end_time - start_time)

defmethod5

(): start_time = clock()

for k in dic.iterkeys():

t = k + dic[k]

end_time = clock()

print

"method 5 costs time is : " , str(end_time - start_time)

defmethod6

(): start_time = clock()

for k, v in zip(dic.iterkeys(), dic.itervalues()) :

t = k + v

end_time = clock()

print

"method 6 costs time is : " , str(end_time - start_time)

if __name__ == '__main__' : method1(), method2(), method3(),method4(),method5(),method6()

第一次執行 python dict_test.py的時間如下:

method 1 costs

time

is :

0.655

method 2 costs

time

is :

5.226

method 3 costs

time

is :

0.64

method 4 costs

time

is :

0.687

method 5 costs

time

is :

0.624

method 6 costs

time

is :

2.028

第二次執行 python dict_test.py的時間如下:

method 1 costs

time

is :

0.639

method 2 costs

time

is :

5.335

method 3 costs

time

is :

0.64

method 4 costs

time

is :

0.687

method 5 costs

time

is :

0.639

method 6 costs

time

is :

2.027

第三次執行 python dict_test.py的時間如下:

method 1 costs

time

is :

0.64

method 2 costs

time

is :

5.336

method 3 costs

time

is :

0.64

method 4 costs

time

is :

0.702

method 5 costs

time

is :

0.655

method 6 costs

time

is :

2.027

第四次執行 python dict_test.py的時間如下:

method 1 costs

time

is :

0.655

method 2 costs

time

is :

5.475

method 3 costs

time

is :

0.671

method 4 costs

time

is :

0.687

method 5 costs

time

is :

0.655

method 6 costs

time

is :

2.027

第五次執行 python dict_test.py的時間如下:

method 1 costs

time

is :

0.64

method 2 costs

time

is :

5.335

method 3 costs

time

is :

0.64

method 4 costs

time

is :

0.702

method 5 costs

time

is :

0.624

method 6 costs

time

is :

2.028

從資料看第1、3、5種遍歷字典的方法在時間上相近,第4種遍歷方法時間上跟上述三種方法相差不大,效能最差的是第二種方法,第6中方法次之。

Python遍歷字典中的鍵

遍歷字典中的鍵 對於餐館中的廚師來說,他們並不想要知道菜的 只需要知道菜名然後將其做出來就行。所以對於廚師來說,我們需要遍歷menu字典中的所有菜名。python為字典型別內建了keys 方法,該方法會將字典裡的鍵遍歷出來,例如 建立並初始化menu選單字典 menu 利用keys 方法遍歷輸出鍵 ...

Python中如何遍歷字典

今天在寫乙個判斷列表中的元素是否與字典中的key值相等的時候,需要用到字典的遍歷,經過查閱資料,知道怎麼遍歷字典的key值 程式如下 這個程式是判斷列表中元素是否與字典中的key值相等,如果相等就將字典的中的元素換成字典中key值對應的value。對於字典的遍歷還有其他的方法,總結如下 分為三種方法...

遍歷字典 十三 Python字典三種遍歷方法

python字典是最基本的資料結構之一,在python中使用非常頻繁,所以對python字典的遍歷非常重要。下面介紹常用的幾種字典遍歷方法。d1 根據key遍歷字典獲取值for key in d1.keys print f key value d1 遍歷value值for value in d1.v...