尽量不在代码中使用太多变量,我想出了下面的代码。看起来很可怕。关于如何很好地格式化它的任何想法?我需要使用更多变量吗?
我经常编写这样的代码,这将有助于了解人们通常采用哪些方法来获得可读代码,同时减少创建变量
exceptions = []
# find all the distinct parent exceptions (sorted) and add to the list
# with their children list
for parent in collection.find(
{'tags': 'exception'}).sort('viewPriority').distinct('parentException'):
group_info = {'groupName': parent,
'children': [{'value': ex['value'],
'label': ex['label'],}
for ex in collection.find({'tags': 'exception',
'parentException': parent}
).sort('viewPriority')],
}
exceptions.append(group_info)