元组键值 [英] Tuple value by key

查看:18
本文介绍了元组键值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从元组中获取价值:

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).

这篇关于元组键值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆