首页 > 其他 > 详细

Looping and dictionaries

时间:2014-07-16 19:19:32      阅读:325      评论:0      收藏:0      [点我收藏+]

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!