【发布时间】:2013-09-19 21:52:03
【问题描述】:
使用以下方法,我可以创建一个值字典:
{ p.id : {'total': p.total} for p in p_list}
这导致{34:{'total':334}, 53:{'total: 123} ... }
我还想从列表中列出一个索引,以便我知道p.id 在哪个位置。我做了一个这样的列表:
c_list = [x for x in range(len(p_list))]
然后尝试查看c 如何也可以作为结果的一部分列出。我想我需要这样的东西:
{ (p,c) for p in p_list for c in c_list}
但是当我尝试实现它时,我无法让 c 成为字典中的值:
{ (p.id, c : {'total': p.total, 'position': c}) for p in p_list for c in c_list}
【问题讨论】:
标签: python list dictionary list-comprehension