Dadep
Dadep

Reputation: 2788

basic python misunderstanding of dict (TypeError: string indices must be integers)

I'm beginning to learn python and I try to make a turing machine app, using pyQt. I get from aQTextEdit some "code" and put it in a dict and get something like :

{'1': {'a': ['s', 'D', '2'], 's': ['s', 'G', '2']}, '2': {'a': ['a', 'D', '1']}}

I have this function where table is a Dict:

def execute_TM(self, table, ruban, etat1):
    self.Ruban.position = 1
    self.table = table
    etatAct = etat1
    while etatAct != 'stop':
        symb = self.Ruban.lire_cellules()
        # print symb
        print self.table
        nvSymb = self.table[etatAct][symb][0]
        self.Ruban.ecrire(nvSymb)
        if table[etatAct][symb][1] == 'D':
            self.Ruban.deplacement_droite()
        if table[etatAct][symb][1] == 'G':
            self.Ruban.deplacement_gauche()
        else:
            print
            "erreur code deplacement"
        etatAct = table[etatAct][symb][2]

And I get this error :

nvSymb = self.table[etatAct][symb][0]
TypeError: string indices must be integers

I have been reading lot of post about this error, and tried different things...But I still don't understand.

Edit : Thanks to your help, I'm trying to understand, so if I have :

table={'1': {'a': ['s', 'D', '2'], 's': ['s', 'G', '2']}, '2': {'a': ['a', 'D', '1']}}

And then I want to get from the main key '1' and from the key 's' the second element of the list : 'G' I can call table['1']['s'][1] so it something like:

table["here it's a string"]["here it's also a string"]["here it's an integer"]

and it work :

>>> table={'1': {'a': ['s', 'D', '2'], 's': ['s', 'G', '2']}, '2': {'a':['a', 'D', '1']}}
>>> etatAct='1'
>>> symb='s'
>>> table[etatAct][symb][1]
'G'

I still don't understand why it do not work in the function....

Edit2 :

Using type() I have found that self.table is not a dict but a PyQt4.QtCore.QStringListanyone know how to easily transform it ?

Upvotes: 0

Views: 2239

Answers (3)

Anthon
Anthon

Reputation: 76792

You should take this:

nvSymb=self.table[etatAct][symb][0]

apart so you can see where you get a string:

tmp = self.table[etatAct]
nvSymb = tmp[symb][0]

and see whether self.table or self.table[etaAct] is a string that gets indexed by a non-integer. Starting with that knowledge that you shoud be able to solve this correcting your input.

Upvotes: 1

SunilThorat
SunilThorat

Reputation: 1748

From your error its clear that one of the indexes of (etatAct, symb) is not an integer but string.

nvSymb=self.table[etatAct][symb][0]
TypeError: string indices must be integers

You can try it by converting it to integers.

 nvSymb=self.table[int(etatAct)][int(symb)][0]

Upvotes: 1

Rahul K P
Rahul K P

Reputation: 16081

You will understand what is the error from this,

In [10]: a = 'Hellooo'
In [11]: print a[0]
H
In [12]: print a['0']
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-12-175cb7ceb755> in <module>()
----> 1 print a['0']

TypeError: string indices must be integers, not str

In your code trying to index string with string instead of dictionary.

Upvotes: 3

Related Questions