什么时候使用字典? [英] When to use dictionaries?
问题描述
我不确定我应该何时在应用程序中使用字典.
I`m not really sure when I should use a dictionary in my applications.
每次我编写一个返回单个值的开关案例时,例如,我应该争取一个dic吗?
Everytime I code a switch case that returns a single value, for example, should I strive for a dic?
谢谢
推荐答案
只要您具有自然的键->值关系,字典就很有用. 在语言词典(键:一个单词的值:翻译列表)或文档索引(键:标题,值:文本正文)中就是这种情况.
A dictionary is useful whenever you have a natural key->value relationship. This is the case in a language dictionary (key:one word value:List of translations), or for a document index (key:title, value:text body).
如果您发现自己编写了一个switch语句,其中每种情况仅负责返回单个值,则您具有这种自然的key-> value关系,应使用字典.
If you find yourself coding a switch statement where each case is only responsible for returning a single value you have such a natural key->value relationship and should use a dictionary.
这篇关于什么时候使用字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!