Convert int into str while in __getitem__ Python 2.7
<p>Okay, i have this string:</p>

<p><code>string = "HelloWorld"</code> </p>

<p>And for this example, I am using a dictionary similar to this:</p>

<p><code>dic = [{'a':'k','b':'i'},{'a':'i','b':'l'},{'a':'x','b':'n'},{'a':'q','b':'o'}]</code></p>

<p>Now.. I need to reference a dictionary from the list, so that I can change the characters in my string. To do so normally I would just do this:</p>

<p><code>dic[#]</code></p>

<p>But in this case I also need a value from that dictionary. Now I tried this:</p>

<p><code>dic[int(char[i[letter]])]</code></p>

<p>But I get the error:</p>

<p><code>'int' object has no attribute '__getitem__'</code></p>

<p>Searching for the error gives me answers here on StackOverflow, but they do not fix my problem.</p>

<p>Here's my best crystal ball guess at what you want.  The keys of your dictionary are only <code>a</code> and <code>b</code>, which aren't any characters in <code>HelloWorld</code>, but if you actually had characters from your string as keys like the following, this is how I'd do the replacement, assuming you want to rotate through the four different dictionaries:</p>

<pre><code>string = "HelloWorld"
D = [{'H':'a','o':'e','l':'i'},{'e':'b','W':'f','d':'j'},{'l':'c','o':'g'},{'l':'d','r':'h'}]
print ''.join(D[i%4][c] for i,c in enumerate(string))
</code></pre>

<p>Output:</p>

<pre><code>abcdefghij
</code></pre>

python python-2.7