元组键值
问题描述
是否可以从元组中获取价值:
Is it possible to get Value out of tuple:
TUPLE = (
('P', 'Shtg1'),
('R', u'Shtg2'),
('D', 'Shtg3'),
)
通过调用像 P
Python 说只有 int 可以用于这种类型的查询"
Python says that only int can be used for this type of 'query'
我不能使用循环(开销太大...)
I can't use loop (too much overhead...)
谢谢!
解决方案
此类查询的规范数据结构是字典:
The canonical data structure for this type of queries is a dictionary:
In [1]: t = (
...: ('P', 'Shtg1'),
...: ('R', u'Shtg2'),
...: ('D', 'Shtg3'),
...: )
In [2]: d = dict(t)
In [3]: d['P']
Out[3]: 'Shtg1'
如果使用元组,则无法避免循环(显式或隐式).
If you use a tuple, there is no way to avoid looping (either explicit or implicit).
相关文章