Tuple value by key

后端 未结 5 1215
滥情空心
滥情空心 2020-12-08 19:21

Is it possible to get Value out of tuple:

TUPLE = (
    (\'P\', \'Shtg1\'),
    (\'R\', u\'Shtg2\'),
    (\'D\', \'Shtg3\'),
)

by calling S

5条回答
  •  难免孤独
    2020-12-08 20:03

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

提交回复
热议问题