2 回答

TA貢獻(xiàn)1909條經(jīng)驗(yàn) 獲得超7個(gè)贊
您的代碼的一個(gè)有趣的事實(shí)是,它看起來(lái)具有檢測(cè)更改背后的價(jià)值。不幸的是,最后一個(gè)值沒(méi)有機(jī)會(huì)被檢測(cè)到,因?yàn)檠h(huán)在那里結(jié)束。
相反,使用兩個(gè)指針,一個(gè)用于當(dāng)前值,一個(gè)用于列表中的下一個(gè)值。使用這樣的條件來(lái)檢測(cè)下一個(gè)的變化(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
這是您的函數(shù)的清理副本:
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貢獻(xiàn)1830條經(jīng)驗(yàn) 獲得超9個(gè)贊
考慮到詳細(xì)的數(shù)據(jù)結(jié)構(gòu),我預(yù)計(jì)效率并不是您真正追求的,因此這似乎是一種相當(dāng)簡(jiǎn)潔且可讀的方式來(lái)獲取您所需要的內(nèi)容:
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)
主要缺點(diǎn)是它會(huì)在每個(gè)列表上循環(huán)多次,但它給你帶來(lái)的是簡(jiǎn)潔性和可讀性。
添加回答
舉報(bào)