0

I'm trying to transcribe my Java classes to Python. The last one was the Warshall Algorithm, but when I run the aplication, I get this error:

C:\Python33\python.exe C:/Users/Joloch/PycharmProjects/Varios/Warshall.py
Inserte un valor para n (la matriz será cuadrada, por lo que es nxn):
3
Inserte un dato en la posición 0 0 :
Inserte un dato en la posición 0 1 :

Traceback (most recent call last):
  File "C:/Users/Joloch/PycharmProjects/Varios/Warshall.py", line 93, in <module>
    Warshall().main()
  File "C:/Users/Joloch/PycharmProjects/Varios/Warshall.py", line 59, in main
    obj.Matriz[x][y] = Dato
IndexError: list assignment index out of range

Process finished with exit code 1

This is the code, I appreciate a lot if you can tell me what I'm doing wrong:

from Tools.Scripts.treesync import raw_input

class Warshall(object):
    pass

    Matriz = [],[]
    Lado = 0

    def __init__(self):
        return

    @classmethod
    def Funcion(self, A, B, C):
        if ((self.Matriz[A][B] == 1) ^ (self.Matriz[A][C] == 1) & (self.Matriz[C][B] == 1)):
            return 1
        else:
            return 0

    def main(self):
        obj = Warshall()
        Dato = 0
        x = 0
        y = 0
        z = 0
        Uno = 0
        Dos = 0
        Tres = 0
        Cuatro = 0
        print("Inserte un valor para n (la matriz será cuadrada, por lo que es nxn): ")
        obj.Lado = int(raw_input(""))
        obj.Matriz = [obj.Lado],[obj.Lado]
        while x < obj.Lado:
            while y < obj.Lado:
                print("Inserte un dato en la posición", x, y,": ")
                try:
                    Dato = int(raw_input())
                except TypeError:
                    print()
                obj.Matriz[x][y] = Dato
                y += 1

            while z <= obj.Lado - 1:
                while Uno <= obj.Lado - 1:
                    while Dos <= obj.Lado - 1:
                        obj.Matriz[Uno][Dos] = obj.Funcion(Uno, Dos, z)
                        Dos += 1
                    Uno += 1
                z += 1

            print()
            print("Matriz de adyacencia correspondiente: ")

            while Tres < obj.Lado:
                while Cuatro < obj.Lado:
                    print(obj.Matriz[Tres][Cuatro])
                    print()
                    Cuatro += 1
                Tres += 1

if __name__ == '__main__':
    Warshall().main()
martineau
  • 119,623
  • 25
  • 170
  • 301
  • Hmmm... Why do you a) bother to define `__init__` when you don't do anything in that method, and b) why is your `main()` function *inside* of your class? – Joel Cornett Feb 09 '13 at 21:12
  • a) Habit, I usually put a constructor in every class where I use methods. b) I'm new at Python, so I thought that it might be like Java, because I saw a couple of examples with main methods. – André Villanueva Escarela Feb 09 '13 at 22:51

1 Answers1

2

This code is structured in a, ah, curious way which makes in hard to sort out what exactly is going on.

The source of your error is that obj.Matriz is a tuple with two length 1 lists in it, not a Lado by Lado by array as you expect. If you print out obj.Matriz you will get ([5], [5]) (if Lado == 5).

Try something more like

obj.Matriz = [[0 for j in range(obj.Lado)] for k in range(obj.Lado)]

which will give you Lado lists of length Lado filled with 0. If you are doing numeric work you might want to look into numpy as well.

tacaswell
  • 84,579
  • 22
  • 210
  • 199
  • The same error, but now on this line: `if ((self.Matriz[A][B] == 1) ^ (self.Matriz[A][C] == 1) & (self.Matriz[C][B] == 1)):` Maybe the causes are the symbols ^ and &... in my Java class that line is: `if ((Matriz[K][Q] == 1) || ((Matriz[K][J] == 1) && (Matriz[J][Q] == 1)))` Which operators are similar to "&&" and "||"? – André Villanueva Escarela Feb 09 '13 at 22:56
  • 1) It is impossible to read code in comments 2) `and` and `or` and 3) that is a sufficiently different question that you should start a new question. – tacaswell Feb 10 '13 at 00:11