Ich versuche, Listen in eine Liste zu sortieren, die Nummern und Namen von Abschnitten, Unterabschnitten und Unterunterabschnitten enthält. Das Programm sieht wie folgt aus:Python heapq Sortierliste falsch?
import heapq
sections = ['1. Section', '2. Section', '3. Section', '4. Section', '5. Section', '6. Section', '7. Section', '8. Section', '9. Section', '10. Section', '11. Section', '12. Section']
subsections = ['1.1 Subsection', '1.2 Subsection', '1.3 Subsection', '1.4 Subsection', '2.1 Subsection', '4.1 My subsection', '7.1 Subsection', '8.1 Subsection', '12.1 Subsection']
subsubsections = ['1.2.1 Subsubsection', '1.2.2 Subsubsection', '1.4.1 Subsubsection', '2.1.1 Subsubsection', '7.1.1 Subsubsection', '8.1.1 Subsubsection', '12.1.1 Subsubsection']
sorted_list = list(heapq.merge(sections, subsections, subsubsections))
print(sorted_list)
Was ich aus, ist dies:
['1. Section', '1.1 Subsection', '1.2 Subsection', '1.2.1 Subsubsection', '1.2.2 Subsubsection', '1.3 Subsection', '1.4 Subsection', '1.4.1 Subsubsection', '2. Section', '2.1 Subsection', '2.1.1 Subsubsection', '3. Section', '4. Section', '4.1 My subsection', '5. Section', '6. Section', '7. Section', '7.1 Subsection', '7.1.1 Subsubsection', '8. Section', '8.1 Subsection', '12.1 Subsection', '8.1.1 Subsubsection', '12.1.1 Subsubsection', '9. Section', '10. Section', '11. Section', '12. Section']
Mein 12. Unterabschnitt und Unterteilabschnitt innerhalb der 8. Abschnitt befindet, nicht 12..
Warum passiert das? Die ursprünglichen Listen sind sortiert, und alles geht gut, anscheinend bis zur Nummer 10.
Ich bin mir nicht sicher, warum das passiert und gibt es eine Möglichkeit, diese besser in einen "Baum" basierend auf den Zahlen in zu sortieren die Listen? Ich baue ein Inhaltsverzeichnis der Art, und dies wird zurückkehren (sobald ich die Liste herauszufiltern)
1. Section
1.1 Subsection
1.2 Subsection
1.2.1 Subsubsection
1.2.2 Subsubsection
1.3 Subsection
1.4 Subsection
1.4.1 Subsubsection
2. Section
2.1 Subsection
2.1.1 Subsubsection
3. Section
4. Section
4.1 My subsection
5. Section
6. Section
7. Section
7.1 Subsection
7.1.1 Subsubsection
8. Section
8.1 Subsection
12.1 Subsection
8.1.1 Subsubsection
12.1.1 Subsubsection
9. Section
10. Section
11. Section
12. Section
Beachten Sie die 12.1 Abs hinter 8.1 Unterabschnitt und 12.1.1 Subsubsection hinter 8.1.1 Subsubsection.
Weil es auf der lexikographischen Reihenfolge der Strings Betrieb - nicht Ihre Versionen "numbers" ... –