Оптимизиция кода Python и рекурсия
Есть исходный список представляющий собой структуру вложенных тегов XML документа:
some_list = ['CadastralBlock', ['ParentCadastralNumbers', ['CadastralNumber']], ['PrevCadastralNumbers', ['CadastralNumber']], 'PreviouslyPosted', 'Name', 'ObjectType', 'AssignationBuilding', ['ElementsConstruct', ['Material']], 'ExploitationChar', 'Floors', 'Area', ['Location', ['FIAS', 'OKATO', 'KLADR', 'OKTMO', 'PostalCode', 'RussianFederation', 'Region', 'District', 'City', 'UrbanDistrict', 'SovietVillage', 'Locality', 'Street', 'Level1', 'Level2', 'Level3', 'Apartment', 'Other', 'Note', 'ReadableAddress']], ['ObjectPermittedUses', ['ObjectPermittedUse']], ['CadastralCost', ['DateValuation', 'DateEntering', 'DateApproval', 'ApplicationDate', 'RevisalStatementDate', 'ApplicationLastDate', ['ApprovalDocument', ['CodeDocument', 'Name', 'Series', 'Number', 'Date', 'IssueOrgan', 'Desc']]]], ['SubBuildings', [['SubBuilding', ['Area', ['Encumbrances', ['Name', 'Type', ['Registration', ['RightNumber', 'RegistrationDate']], ['Document', ['CodeDocument', 'Name', 'Series', 'Number', 'Date', 'IssueOrgan', 'Desc']]]]]]]], ['FlatsCadastralNumbers', ['CadastralNumber']], ['CarParkingSpacesCadastralNumbers', ['CadastralNumber']], ['UnitedCadastralNumber', ['CadastralNumber', 'Purpose', 'Name']], ['FacilityCadastralNumber', ['CadastralNumber', 'Purpose', 'Name']], ['CulturalHeritage', [['InclusionEGROKN', ['RegNum', 'ObjCultural', 'NameCultural']], ['AssignmentEGROKN', ['RegNum', 'ObjCultural', 'NameCultural']], 'RequirementsEnsure', ['Document', ['CodeDocument', 'Name', 'Series', 'Number', 'Date', 'IssueOrgan', 'Desc']]]]]
Написал функцию, обработчик исходного списка some_list, которая выводит структуру тегов в линейную последовательность:
def process_list(lst):
for i in lst:
if isinstance(i, str):
print(i)
elif isinstance(i, list):
if len(i) > 1 and isinstance(i[1], list):
for item in i[1]:
if isinstance(item, list):
for subitem in item[1]:
if isinstance(subitem, list):
for subsubitem in subitem[1]:
if isinstance(subsubitem, list):
for subsubsubitem in subsubitem[1]:
print(i[0], item[0], subitem[0], subsubitem[0], subsubsubitem)
else:
print(i[0], item[0], subitem[0], subsubitem)
else:
print(i[0], item[0], subitem)
else:
print(i[0], item)
else:
print(i[0])
process_list(some_list)
Вывод функции process_list() следующий:
CadastralBlock
ParentCadastralNumbers CadastralNumber
PrevCadastralNumbers CadastralNumber
PreviouslyPosted
Name
ObjectType
AssignationBuilding
ElementsConstruct Material
ExploitationChar
Floors
Area
Location FIAS
Location OKATO
Location KLADR
Location OKTMO
Location PostalCode
Location RussianFederation
Location Region
Location District
Location City
Location UrbanDistrict
Location SovietVillage
Location Locality
Location Street
Location Level1
Location Level2
Location Level3
Location Apartment
Location Other
Location Note
Location ReadableAddress
ObjectPermittedUses ObjectPermittedUse
CadastralCost DateValuation
CadastralCost DateEntering
CadastralCost DateApproval
CadastralCost ApplicationDate
CadastralCost RevisalStatementDate
CadastralCost ApplicationLastDate
CadastralCost ApprovalDocument CodeDocument
CadastralCost ApprovalDocument Name
CadastralCost ApprovalDocument Series
CadastralCost ApprovalDocument Number
CadastralCost ApprovalDocument Date
CadastralCost ApprovalDocument IssueOrgan
CadastralCost ApprovalDocument Desc
SubBuildings SubBuilding Area
SubBuildings SubBuilding Encumbrances Name
SubBuildings SubBuilding Encumbrances Type
SubBuildings SubBuilding Encumbrances Registration RightNumber
SubBuildings SubBuilding Encumbrances Registration RegistrationDate
SubBuildings SubBuilding Encumbrances Document CodeDocument
SubBuildings SubBuilding Encumbrances Document Name
SubBuildings SubBuilding Encumbrances Document Series
SubBuildings SubBuilding Encumbrances Document Number
SubBuildings SubBuilding Encumbrances Document Date
SubBuildings SubBuilding Encumbrances Document IssueOrgan
SubBuildings SubBuilding Encumbrances Document Desc
FlatsCadastralNumbers CadastralNumber
CarParkingSpacesCadastralNumbers CadastralNumber
UnitedCadastralNumber CadastralNumber
UnitedCadastralNumber Purpose
UnitedCadastralNumber Name
FacilityCadastralNumber CadastralNumber
FacilityCadastralNumber Purpose
FacilityCadastralNumber Name
CulturalHeritage InclusionEGROKN RegNum
CulturalHeritage InclusionEGROKN ObjCultural
CulturalHeritage InclusionEGROKN NameCultural
CulturalHeritage AssignmentEGROKN RegNum
CulturalHeritage AssignmentEGROKN ObjCultural
CulturalHeritage AssignmentEGROKN NameCultural
CulturalHeritage RequirementsEnsure
CulturalHeritage Document CodeDocument
CulturalHeritage Document Name
CulturalHeritage Document Series
CulturalHeritage Document Number
CulturalHeritage Document Date
CulturalHeritage Document IssueOrgan
CulturalHeritage Document Desc
Функция process_list() скорее всего представляет не оптимальное решение проблемы. Как можно ее оптимизировать и сделать рекурсивной с учетом предположения о неизвестной глубине структуры списка some_list?
Ответы (2 шт):
Автор решения: Oopss
→ Ссылка
def process_list(lst):
for i in lst:
if isinstance(i, list):
process_list(i)
else:
print(i)
some_list = ['CadastralBlock', ['ParentCadastralNumbers', ['CadastralNumber']], ['PrevCadastralNumbers', ['CadastralNumber']], 'PreviouslyPosted', 'Name', 'ObjectType', 'AssignationBuilding', ['ElementsConstruct', ['Material']], 'ExploitationChar', 'Floors', 'Area', ['Location', ['FIAS', 'OKATO', 'KLADR', 'OKTMO', 'PostalCode', 'RussianFederation', 'Region', 'District', 'City', 'UrbanDistrict', 'SovietVillage', 'Locality', 'Street', 'Level1', 'Level2', 'Level3', 'Apartment', 'Other', 'Note', 'ReadableAddress']], ['ObjectPermittedUses', ['ObjectPermittedUse']], ['CadastralCost', ['DateValuation', 'DateEntering', 'DateApproval', 'ApplicationDate', 'RevisalStatementDate', 'ApplicationLastDate', ['ApprovalDocument', ['CodeDocument', 'Name', 'Series', 'Number', 'Date', 'IssueOrgan', 'Desc']]]], ['SubBuildings', [['SubBuilding', ['Area', ['Encumbrances', ['Name', 'Type', ['Registration', ['RightNumber', 'RegistrationDate']], ['Document', ['CodeDocument', 'Name', 'Series', 'Number', 'Date', 'IssueOrgan', 'Desc']]]]]]]], ['FlatsCadastralNumbers', ['CadastralNumber']], ['CarParkingSpacesCadastralNumbers', ['CadastralNumber']], ['UnitedCadastralNumber', ['CadastralNumber', 'Purpose', 'Name']], ['FacilityCadastralNumber', ['CadastralNumber', 'Purpose', 'Name']], ['CulturalHeritage', [['InclusionEGROKN', ['RegNum', 'ObjCultural', 'NameCultural']], ['AssignmentEGROKN', ['RegNum', 'ObjCultural', 'NameCultural']], 'RequirementsEnsure', ['Document', ['CodeDocument', 'Name', 'Series', 'Number', 'Date', 'IssueOrgan', 'Desc']]]]]
process_list(some_list)
CadastralBlock
ParentCadastralNumbers
CadastralNumber
PrevCadastralNumbers
CadastralNumber
PreviouslyPosted
Name
ObjectType
AssignationBuilding
ElementsConstruct
Material
ExploitationChar
Floors
Area
Location
FIAS
OKATO
KLADR
OKTMO
PostalCode
RussianFederation
Region
District
City
UrbanDistrict
SovietVillage
Locality
Street
Level1
Level2
Level3
Apartment
Other
Note
ReadableAddress
ObjectPermittedUses
ObjectPermittedUse
CadastralCost
DateValuation
DateEntering
DateApproval
ApplicationDate
RevisalStatementDate
ApplicationLastDate
ApprovalDocument
CodeDocument
Name
Series
Number
Date
IssueOrgan
Desc
SubBuildings
SubBuilding
Area
Encumbrances
Name
Type
Registration
RightNumber
RegistrationDate
Document
CodeDocument
Name
Series
Number
Date
IssueOrgan
Desc
FlatsCadastralNumbers
CadastralNumber
CarParkingSpacesCadastralNumbers
CadastralNumber
UnitedCadastralNumber
CadastralNumber
Purpose
Name
FacilityCadastralNumber
CadastralNumber
Purpose
Name
CulturalHeritage
InclusionEGROKN
RegNum
ObjCultural
NameCultural
AssignmentEGROKN
RegNum
ObjCultural
NameCultural
RequirementsEnsure
Document
CodeDocument
Name
Series
Number
Date
IssueOrgan
Desc
Автор решения: Stanislav Volodarskiy
→ Ссылка
def print_list(nested_list):
stack = []
def traverse(item):
match item:
case [str() as s, list() as items]:
stack.append(s)
traverse(items)
stack.pop()
case list() as items:
for i in items:
traverse(i)
case _:
print(*stack, item)
traverse(nested_list)
some_list = [
'CadastralBlock',
['ParentCadastralNumbers', ['CadastralNumber']],
['PrevCadastralNumbers', ['CadastralNumber']],
'PreviouslyPosted',
'Name',
'ObjectType',
'AssignationBuilding',
['ElementsConstruct', ['Material']],
'ExploitationChar',
'Floors',
'Area',
['Location', [
'FIAS', 'OKATO', 'KLADR', 'OKTMO', 'PostalCode',
'RussianFederation', 'Region', 'District', 'City', 'UrbanDistrict',
'SovietVillage', 'Locality', 'Street', 'Level1', 'Level2',
'Level3', 'Apartment', 'Other', 'Note', 'ReadableAddress'
]],
['ObjectPermittedUses', ['ObjectPermittedUse']],
['CadastralCost', [
'DateValuation', 'DateEntering', 'DateApproval', 'ApplicationDate',
'RevisalStatementDate', 'ApplicationLastDate',
['ApprovalDocument', [
'CodeDocument', 'Name', 'Series', 'Number', 'Date', 'IssueOrgan',
'Desc'
]]
]],
['SubBuildings', [
['SubBuilding', [
'Area', [
'Encumbrances', [
'Name', 'Type',
['Registration', ['RightNumber', 'RegistrationDate']],
['Document', [
'CodeDocument', 'Name', 'Series', 'Number', 'Date',
'IssueOrgan', 'Desc'
]]
]
]
]]
]],
['FlatsCadastralNumbers', ['CadastralNumber']],
['CarParkingSpacesCadastralNumbers', ['CadastralNumber']],
['UnitedCadastralNumber', ['CadastralNumber', 'Purpose', 'Name']],
['FacilityCadastralNumber', ['CadastralNumber', 'Purpose', 'Name']],
['CulturalHeritage', [
['InclusionEGROKN', ['RegNum', 'ObjCultural', 'NameCultural']],
['AssignmentEGROKN', ['RegNum', 'ObjCultural', 'NameCultural']],
'RequirementsEnsure',
['Document', [
'CodeDocument', 'Name', 'Series', 'Number', 'Date', 'IssueOrgan',
'Desc'
]]
]]
]
print_list(some_list)