2 回答
TA贡献1909条经验 获得超7个赞
您的代码的一个有趣的事实是,它看起来具有检测更改背后的价值。不幸的是,最后一个值没有机会被检测到,因为循环在那里结束。
相反,使用两个指针,一个用于当前值,一个用于列表中的下一个值。使用这样的条件来检测下一个的变化(lookahead)
for index, outer in enumerate(outer_list):
next_outer_source = outer_list[index + 1] if index < len(outer_list) - 1 else None
show_outer_header = current_outer_name != outer.name or current_outer_source != outer.source
show_inner_values = next_outer_source is None or outer.name != next_outer_source.name or outer.source != next_outer_source.source
这是您的函数的清理副本:
def loop_over(outer_list, inner_list):
current_outer_name = None
current_outer_source = None
current_inner_name = None
current_inner_source = None
prev_outer_source = None
for index, outer in enumerate(outer_list):
next_outer_source = outer_list[index + 1] if index < len(outer_list) - 1 else None
show_outer_header = current_outer_name != outer.name or current_outer_source != outer.source
show_inner_values = next_outer_source is None or outer.name != next_outer_source.name or outer.source != next_outer_source.source
# print outer header
if show_outer_header:
print('\n{} ({})'.format(outer.name, outer.source))
print('=' * 15)
current_outer_name, current_outer_source = outer.name, outer.source
# print outer value
print('* [{}]: . "{}"'.format(outer.thing, outer.description))
# print inner values
if show_inner_values:
current_outer_name = outer.name
current_outer_source = outer.source
for inner in [x for x in inner_list if x.name == current_outer_name and x.source == current_outer_source]:
if current_inner_name is None:
print('\n{} ({})'.format(inner.in_name, inner.in_source))
print('-' * 15)
current_inner_name = inner.in_name
current_inner_source = inner.in_source
if inner.in_name != current_inner_name or inner.in_source != current_inner_source:
print('\n{} ({})'.format(inner.in_name, inner.in_source))
print('-' * 15)
current_inner_name = inner.in_name
current_inner_source = inner.in_source
print('* [{}]: . "{}"'.format(inner.thing, inner.description))
输出:
name1 (source1)
===============
* [name1-foo1]: . "description1"
* [name1-foo2]: . "description2"
* [name1-foo3]: . "description3"
subname1 (subsource1)
---------------
* [name1-sub1-bar1]: . "description1"
* [name1-sub1-bar2]: . "description2"
* [name1-sub1-bar3]: . "description3"
subname2 (subsource1)
---------------
* [name1-sub2-bar1]: . "description1"
* [name1-sub2-bar2]: . "description2"
* [name1-sub2-bar3]: . "description3"
name2 (source1)
===============
* [name2-foo1]: . "description1"
* [name2-foo2]: . "description2"
* [name2-foo3]: . "description3"
subname3 (subsource1)
---------------
* [name2-sub3-bar1]: . "description1"
* [name2-sub3-bar2]: . "description2"
* [name2-sub3-bar2]: . "description3"
name3 (source1)
===============
* [name3-foo1]: . "description1"
* [name3-foo2]: . "description2"
subname4 (subsource1)
---------------
* [name3-sub4-bar1]: . "description1"
* [name3-sub4-bar2]: . "description2"
* [name3-sub4-bar2]: . "description3"
subname5 (subsource1)
---------------
* [name3-sub5-bar1]: . "description1"
* [name3-sub5-bar2]: . "description2"
* [name3-sub5-bar3]: . "description3"
subname6 (subsource1)
---------------
* [name3-sub6-bar1]: . "description1"
* [name3-sub6-bar2]: . "description2"
* [name3-sub6-bar3]: . "description3"
TA贡献1830条经验 获得超9个赞
考虑到详细的数据结构,我预计效率并不是您真正追求的,因此这似乎是一种相当简洁且可读的方式来获取您所需要的内容:
def print_outer_and_inner(outer_recs, inner_recs):
for name, source in {(o_rec.name, o_rec.source): None for o_rec in outer_recs}:
print(f'{name} ({source})')
print('=' * 15)
for o_rec in outer_recs:
if o_rec.name == name:
print(f'* [{o_rec.thing}]: . "{o_rec.description}"')
print()
for sub_name, sub_source in {(i_rec.in_name, i_rec.in_source): None for i_rec in inner_recs}:
print(f'{sub_name} ({sub_source})')
print('-' * 15)
for i_rec in inner_recs:
if i_rec.in_name == sub_name:
print(f'* [{i_rec.thing}]: . "{i_rec.description}"')
print()
print_outer_and_inner(o, i)
主要缺点是它会在每个列表上循环多次,但它给你带来的是简洁性和可读性。
添加回答
举报