访问元组的第二个元素?

时间:2017-10-05 15:24:36

标签: python django

以下是我打印Meta.REQUEST_DOCUMENT_TYPE_CHOICES

时的内容
((u'void_cheque', <django.utils.functional.__proxy__ at 0x7fbc6951a4d0>),
 (u'pay_stub', <django.utils.functional.__proxy__ at 0x7fbc6951a510>),
 (u'bank_statement', <django.utils.functional.__proxy__ at 0x7fbc6951a550>),
 (u'bank_statement_60', <django.utils.functional.__proxy__ at 0x7fbc6951a5d0>),
 (u'csst_statement', <django.utils.functional.__proxy__ at 0x7fbc6951a650>),
 (u'saaq_statement', <django.utils.functional.__proxy__ at 0x7fbc6951a6d0>),
 (u'cara_statement', <django.utils.functional.__proxy__ at 0x7fbc6951a750>),
 (u'insurance_letter', <django.utils.functional.__proxy__ at 0x7fbc6951a7d0>),
 (u't4', <django.utils.functional.__proxy__ at 0x7fbc6951a850>),
 (u'welfare_chart', <django.utils.functional.__proxy__ at 0x7fbc6951a8d0>),
 (u'raqp_chart', <django.utils.functional.__proxy__ at 0x7fbc6951a950>),
 (u'customer_id', <django.utils.functional.__proxy__ at 0x7fbc6951a9d0>),
 (u'proof_of_residence',
  <django.utils.functional.__proxy__ at 0x7fbc6951aa50>),
 (u'bankruptcy_proof', <django.utils.functional.__proxy__ at 0x7fbc6951aad0>),
 (u'consumer_proposal', <django.utils.functional.__proxy__ at 0x7fbc6951ab50>),
 (u'signed_contract', <django.utils.functional.__proxy__ at 0x7fbc6951abd0>))

我有这种数据结构,我想只使用<django.utils.functional.__proxy__ at 0x7fbc6951a4d0>访问那里的第二个元素void_cheque。我怎么能这样做?

更新

REQUEST_DOCUMENT_TYPE_CHOICES = (
    ('void_cheque',         _('Void Cheque')),
    ('pay_stub',            _('Pay Stub')),
    ('bank_statement',      _('Bank Statement (31 days)')),
    ('bank_statement_60',   _('Bank Statement (60 days)')),
    ('csst_statement',      _('CSST Statement')),
    ('saaq_statement',      _('SAAQ Statement')),
    ('cara_statement',      _('CARA Statement')),
    ('insurance_letter',    _('Insurance Letter')),
    ('t4',                  _('T4')),
    ('welfare_chart',       _('Welfare Chart')),
    ('raqp_chart',          _('RAQP Chart')),
    ('customer_id',         _('Customer ID')),
    ('proof_of_residence',  _('Proof Of Residence')),
    ('bankruptcy_proof',    _('Bankruptcy Proof')),
    ('consumer_proposal',   _('Consumer Proposal')),
    ('signed_contract',     _('Signed Contract')),
)

3 个答案:

答案 0 :(得分:2)

最简单的方法是首先将元组列表转换为字典,然后使用括号将其编入索引:

d = dict(...)
d['void_cheque']

示例:

>>> tuples = ((u'void_cheque', 42), (u'pay_stub', 1))
>>> d = dict(tuples)
>>> d
{'void_cheque': 42, 'pay_stub': 1}
>>> d['void_cheque']
42

答案 1 :(得分:1)

您的问题与发送简单字符串的代理类型对象的django翻译有关。 您在文件顶部使用from django.utils.translation import ugettext_lazy as _ 因此,在文件顶部添加新导入,如:

from django.utils.translation import ugettext

用ugettext更新下面的元组,之后将返回元组中的实际字符串

REQUEST_DOCUMENT_TYPE_CHOICES = (
    ('void_cheque',         ugettext('Void Cheque')),
    ('pay_stub',            ugettext('Pay Stub')),
    ('bank_statement',      ugettext('Bank Statement (31 days)')),

答案 2 :(得分:0)

我相信你需要做的只是:

Meta.REQUEST_DOCUMENT_TYPE_CHOICES[0][1]

结果你得到Meta.REQUEST_DOCUMENT_TYPE_CHOICES。 我们只是accessed元组的第一个元素,然后访问它的第二个元素。