码迷,mamicode.com
首页 > 其他好文 > 详细

Looping and dictionaries

时间:2014-07-15 23:43:56      阅读:527      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   strong   2014   for   

If you use a dictionary in a for statement, it traverses the keys of the dictionary. For example, print_hist prints each key and the corresponding value:

bubuko.com,布布扣                       

Print dictionary in alphabetical order:

bubuko.com,布布扣 

 

Reverse lookup

Given a dictionary d and a key k, it is easy to find the corresponding value v = d[k]. This operation is called lookup. But what if you have v and you want to find k? You have two problems: first, there might be more than one key that maps to the value v. Depending on the application, you might be able to pick one, or you might have to make a list that contains all of them. Second, there is no simple syntax to do a reverse lookup; you have to search.

bubuko.com,布布扣 

 

A reverse lookup is much slower than a forward lookup; if you have to do it often, or if the dictionary gets big, the performance of your program will suffer.

 

 

from Thinking in Python

Looping and dictionaries,布布扣,bubuko.com

Looping and dictionaries

标签:style   blog   http   strong   2014   for   

原文地址:http://www.cnblogs.com/ryansunyu/p/3844602.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!