Search code examples
pythonlevenshtein-distance

Stop nested for-loop after condition is true for the first time


I have two lists. I want to compare with levenshtein-distance. The first list (raw_1) is a nested list. I want to compare the second list (List) with each item of the nested lists in (raw_1). If the difference between one item of an element (nested list) in raw_1 and one of the compare (compare in code) of List is 3, then it should add the compare to a third list (column_1). After that first time that the condition is true it should start with the next element of raw_1.

I have already tried to break the different for-loops, but it always ended with a lot different outputs which aren't like this I wanted.

column_1 = []
for element in raw_1:
    for item in element:
        for compare in List:
            if levenshtein(item, compare) < 4:
                column_1.append(item.replace(item, compare))
column_1

I currently get just the output with all items compared with compare and replaced, but i just want to have the first item with that condition to be printed in my list column_1 I hope somebody could help

maybe that would be more likely to understand: I have: the original lists are:

List = ['Katharinenstr.', 'Jakobstr.', 'Wilhelmsplatz', 'Hauptstätterstr.',
        'Schlosserstr.', 'Torstr.', 'Kreuzstr.', 'Brückenstr.',
        'Obere Bachstr.', 'Holzstr.', 'Rosenstr.', 'Wilhelm-Murr-Str.',
        'Esslinger Str.', 'Danziger Freiheit', 'Wagnerstr.', 'Leonhardsplatz']

raw_1 = [['n', 'j', 't', 't', 'k', '----', '--------------------------\n'],
         ['bvyrfL', 'A', 's:', '2.44:', 'W', 'A', '¦', 'Z', '-', 'S', '5\n'],
         ["'", 'M', 'S', 'J', 'I', '-', '’-f', '|', 'p', '2\n'],
         ['#', 'J', '•\n'], ['-', '4TZ-"', 'j\n'], ['j:', '-', '"\n'], ['.\n'], ['k\n'],
         ['Hu¨i\n'], ['.\n'], ['¦\n'], ['J', 'M', ':', '|>|P\n'], [',\n'], ["'\n"], ['.\n'],
         ['--------', '---------', '', ';---------------------', '—', '---------------------------------------------------A---------\n'],
         ['\n'], ['J', '|', '^', 'f', 'f', '.', '^', '-', '2', '^', '10/?--', '1\n'],
         ['V', 'Film', '2\n'], ['Aufnahme®', '*te*t', 'Kittel', '(Film', '1', 'ist', 'Picht', 'vorhanden)', 'B', '@', 'a', 'i', 'r', 'k', 'A', 'I\n'],
         ['Dai-ra', '22.8.42', 'Zeit', '„U-JLIÜ', 'Uhr', '|\n'],
         ['F', 'H', '®', 'Hr.', 'Strasse».', 'Platä', 'zmemism.', '|', 'Bemerkung®', 's,', 'I\n'],
         ['1', 'Katharinenstr.', 'FaUobstr.', 'u,Wilhelmsplatz', 'Nordseite', 'I\n'],
         ['2', 'i', 'Iakobstr.', 'Katharinenstr.u.Haupt-', 'Gesamtbild', '1\n'], ['stätterstr.', 'I\n'],
         ['3', 'Katharinenstr.', 'Silhelrnsplatz', 'u.Jakobskr,', 'Nordseite', 'I\n'],
         ['4', 'ilhelmsplatz', '|', 'Katharinen-u.Hauptstätterstr,', 'Nordostseite', 'I\n'],
         ['5', 'jHauptstätterstr,', 'N', 'r', '.', '1', '4', 'u.', 'Chrlstosstr.', 'Wilhelmsplatz', 'Nordwest-', 'I\n'],
         ['|', 'seit«', 'I\n'], ['6', '"', 'von', 'Wilhelmsplatz', 'nach', 'Tu¨dwiesten', 'I\n'],
         ['7', 'Wilhelmsplatz', 'alter', 'Marktbrunnen,', 'SUdwestjseite', 'I\n'], ['8', '"', 'Nr.10', '-', '12', 'SUdwestseite', 'I\n'],
         ['9', 'Schlosscrstr.', 'Einblick', 'von', 'Wilhelms^latz', 'I\n'], ['10', 'Houptstcktterstr.1^', 'Uhlandapotheke', 'I\n'],
         ['11', ';', '"', ':', 'Wilhelmsplatz', '«.Kaufhaus', ';', 'Nordwestseite', 'I\n'], ['jBreuninger', 'I\n'],
         ['.12', '!', 'Torftr.', '[Einblick', 'v.Hauptstätterstr.', '|', 'I\n'], ['13', 'iHauptstätterstr.jNr.50', '-', '36', 'I', 'Nordwestseite', 'I\n'],
         ['14', '"', 'iNr.34', 'und', 'Bru¨ckenstr.', '"', 'I\n'], ['1', '5', '"', '5', 'o', 'I\n'],
         ['16', 'I', 'Hauptstätterstr.', ';', "Hs.i'ir.24", 'und', 'Hs.Nr.2', '|', 'Nordwestseite', 'I\n'],
         ['17', 'IBru¨ckenftr.', 'Einblick', 'von', 'Haupt', 's', 't', 'a', 't', 't', '!', 'I\n'],
         ['18', 'I', 'Hauptstätterftr.Ö', 'Einblick', 'Marktstr.', 'I\n'], ['j', 'I0', 'bere', 'Bach', 'Str.2', 'i', 'I\n'],
         ['IHolzstr.', 'Wagnerstr.u.Danziger', 'Frei-', 'Nordwestseite', 'j\n'], ['heit', 'I\n'],
         ['20', 'IHolzstr.', 'Haupt-', 'Blick', 'nach', 'SUdwesten', '1', 'I\n'], ['I', 'stätterstr.', '^', 'I\n'],
         ['21', 'Rosenstr.', 'Holz-u.Untere', 'Bach', 'Str.', 'Su¨dseite', '|\n'], ['22', 'Holzstr.2', 'u.', '|', 'Lederstr.', 'Nordseite', 'I\n'],
         ['Lederstr.', 'I\n'], ['23', 'tilh.lcurr', 'Str.', 'Holz-', 'u.Untere', 'Bach', 'Str.', 'Gestapo', '|\n'],
         ['24', 'Holzstr.', '|', 'Danziger', 'Freiheit', 'und', 'Kauf-', 'NordwestseiteM;', '*', 'I\n'], ['j', 'haus', 'Breuninger', "'»", 'I\n'],
         ['25', 'Eßlingerstr.', 'j', 'Danz.Freiheit', 'u.Brennerftr,', 'I\n'], ['26', 'Danz.Freiheit', 'Holz-', 'u.EPlinger', 'Str.', 'Su¨dseite', 'I\n'],
         ['27', '|Eßlinger', 'Str.', 'Danz.Freiheit', 'u.Brennerstr.', 'Westseite', 'I\n'], ['28', 'IRosenstr.', 'j', 'Eßlinger', '8', 'tr»u,', 'Holzstr.', '|\n'],
         ['29', 'i', 'Eßlinger', 'Str.', "'", 'Brenner-', 'und', 'arosenftr.', 'j', 'Westseite', '1\n'], ['H\n'], ['------—', '¦', '¦', 'mk', '¦', '¦', '-', '¦', '^\n'],
         ['I', 'AufnahssjäHRa#i', 't.', '..', 'oo', '8', 'A2', 'tttei', 'm', '1-1,2«', '15', 'Uhr', 'jn?', '®-', 'a-', 'a', '*¦', 'vk', '8\n'],
         ['|', 'Fii®Sr', '<j', 'Strasse', '«Plsrts', 'j', 'wleolmg', 'SemsTkungtm\n'], ['I', '30', 'jEPlinget', 'Str.', 'Wagnerstr.u.', 'Leonhardsplatz', 'Westseite\n'],
         ['I', '31', 'bagnerstr.', 'Eßlinger-', 'und', 'Holzstr.', 'Su¨dseite\n'], ['I', '32', 'Eßlinger', 'Str.', 'Wagnerstr.', 'u.', 'Hs.Nr.19', 'Nordwestseite\n'],
         ['I', '33', 'jLeonhardsplatz', '!Blick', 'gegen', 'Hauptstätterstr.', '|\n'], ['I', '34', '*', 'Holz-', 'und', 'Eßlinger', 'Str.\n'],
         ['I', '35', '"\'', 'hinter', 'der', 'Leonhardskitche', 'Nordwestseite\n'], ['I', '36', '"', '|', '"', 'Gustav', 'Siegle', 'Haus', '"\n'],
         ['[', '3', '7', '"', 'Gustav', 'Siegle', 'Haus', 'u.Leon-', '|', '"\n'], ['1I', '.<hardskirche\n'], ['1']]

output should be something like this : column_1: ['Katharinenstr', 'Jakobstr.', 'Katharinenstr.', 'Wihlhelmsplatz', 'Hauptstätterstr.'] Just a little part of the OUtput


Solution

  • Use a flag and add a condition to each loop.

    for element in raw_1:
        found = False
        for item in element:
            for compare in List:
                if levenshtein(item, compare) < 4:
                    column_1.append(item.replace(item, compare))
                    found = True
                    break
            if found: break