[英]How can a list comprehension do this
In this code below, a list is being iterated though, and a dictionary built of 2 values for each entry in the list. 在下面的这段代码中,将迭代一个列表,并为列表中的每个条目构建一个由2个值构成的字典。 The dictionaries and then added to a new list.
然后将词典添加到新列表中。
The code below works, see below for the results of the print statements. 下面的代码有效,请参见下面的print语句结果。
But is it possible to do this in a cleaner way, perhaps with a list comprehension? 但是,是否有可能以一种更清晰的方式做到这一点,也许有了列表理解?
ccdl_rows = ccdl_obj.browse(self.cr, self.uid, ccdl_ids)
print 'ccdl_rows:', ccdl_rows
dist_lines = []
for ccdl in ccdl_rows:
dist_line = {}
dist_line['destination'] = ccdl.destination_id.code
dist_line['cost_center'] = ccdl.analytic_id.code
dist_lines.append(dist_line)
print 'dist_lines: ', dist_lines
ccdl_rows: [browse_record(cost.center.distribution.line, 12), browse_record(cost.center.distribution.line, 13), browse_record(cost.center.distribution.line, 14)]
dist_lines: [{'cost_center': u'BI133', 'destination': u'SUP'}, {'cost_center': u'MW109', 'destination': u'SUP'}, {'cost_center': u'BI196', 'destination': u'SUP'}]
Of course: 当然:
dist_lines = [{'cost_center': ccdl.analytic_id.code,
'destination': ccdl.destination_id.code}
for ccdl in ccdl_rows]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.