我有以下列表和词典:
[u'customer_id', u'bank_statement', u'pay_stub']
和
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')),
)
我已经知道我可以通过这种方式访问每个元素
list = dict(Meta.REQUEST_DOCUMENT_TYPE_CHOICES)
list['void_cheque']
此问题的目的是将第一个列表转换为
['Void Cheque', 'Bank Statement (31 days)', 'Pay Stub']
如果用短线,我可以将第一个列表映射到使用词典的最后一个列表中吗?我知道我可以使用简单的for
语句来完成它,但是我希望在一行中对它进行编码,这样我就可以将它返回到函数内... return your_code
答案 0 :(得分:2)
不是初始化的1-liner,但你可以使用简单的列表理解来获得它:
things = [u'customer_id', u'bank_statement', u'pay_stub']
types = dict(Meta.REQUEST_DOCUMENT_TYPE_CHOICES) # dont use `list` as variable name
new_things = [types[thing] for thing in things if thing in types]
答案 1 :(得分:2)
您可以使用map
:
choices = [u'customer_id', u'bank_statement', u'pay_stub']
choices = list(map(dict(Meta.REQUEST_DOCUMENT_TYPE_CHOICES).get, choices))