Seite 1 von 1

"IndexError: list index out of range"

Verfasst: Freitag 8. Juni 2012, 00:30
von StefanLawl
Hey Leute. Ich habe mal aus Neugier (bin neu mit Python) versucht, ein Skript zu schreiben, welches alle möglichen Kombinationen von den Buchstaben A-Z ausgibt (bis zu 3 Stellen, also "A bis Z", "A bis Z + A bis Z" und "A bis Z + A bis Z + A bis Z").

Es funktioniert soweit auch alles, bis auf einen Fehler ganz am Ende, sobald das Skript durchgelaufen ist.

Code: Alles auswählen

Traceback (most recent call last):
  File "C:/Users/Stefan/Desktop/Python/asdasd.py", line 42, in <module>
    loop3(x,y)
  File "C:/Users/Stefan/Desktop/Python/asdasd.py", line 22, in loop3
    print atoz[x] + atoz[x2] + atoz[x3]
IndexError: list index out of range
Dieser Fehler erscheint wirklich erst ganz am Ende, sobald alle Kombinationen durchgelaufen sind (hier die Ausgabe: http://www.pastebin.com/bHRamh8x).

Der Quellcode:

Code: Alles auswählen

atoz = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
x = 0
x2 = 0
x3 = 0
y = 25

def loop1(x, y):
    while x<y:
        print atoz[x]
        x += 1

def loop2(x, y):
    while x<y:
        print atoz[x] + atoz[x2]
        x += 1

def loop3(x, y):
    while x<y:
        print atoz[x] + atoz[x2] + atoz[x3]
        x += 1

loop1(x,y)

x = 0

while x2<y:
    loop2(x,y)
    x2+=1

x = 0
x2 = 0

while x3<y:
    while x2<y:
        loop3(x,y)
        x2+=1
        if x2==y:
            x2=0
            x3+=1
Danke schon einmal für die Antworten und Entschuldigung, wenn ich was falsch gemacht habe. Ist mein erster Beitrag :/

Re: "IndexError: list index out of range"

Verfasst: Freitag 8. Juni 2012, 00:41
von jbs
Ich denke dein x2 ist immer kleiner als y, da du es wieder auf 0 setzt.

Dein Schleifendesign ist etwas kaputt.

Re: "IndexError: list index out of range"

Verfasst: Freitag 8. Juni 2012, 01:11
von StefanLawl
Ui, schnelle Antwort. Danke, ich schau's mir mal an :D

Gelöst!

Habe am Ende einfach noch folgendes eingefügt:

Code: Alles auswählen

            if x3 == y:
                break
Danke! :)

Re: "IndexError: list index out of range"

Verfasst: Freitag 8. Juni 2012, 07:11
von BlackJack

Code: Alles auswählen

from itertools import chain, product
from string import ascii_uppercase


def main():
    for characters in chain.from_iterable(
        product(*([ascii_uppercase] * i)) for i in xrange(1, 4)
    ):
        print ''.join(characters)


if __name__ == '__main__':
    main()

Re: "IndexError: list index out of range"

Verfasst: Freitag 8. Juni 2012, 16:05
von StefanLawl
BlackJack hat geschrieben:

Code: Alles auswählen

from itertools import chain, product
from string import ascii_uppercase


def main():
    for characters in chain.from_iterable(
        product(*([ascii_uppercase] * i)) for i in xrange(1, 4)
    ):
        print ''.join(characters)


if __name__ == '__main__':
    main()
Ich hab's mal eben ausprobiert. Wow, ich hab noch viel zu lernen :D

Re: "IndexError: list index out of range"

Verfasst: Freitag 8. Juni 2012, 17:35
von pillmuncher
Ähnlich BlackJacks Lösung, nur einfacher:

Code: Alles auswählen

from itertools import product
from string import ascii_uppercase


def main():
    for i in xrange(4):
        for each in product(ascii_uppercase, repeat=i):
            print ''.join(each)


if __name__ == '__main__':
    main()