Skip to content Skip to sidebar Skip to footer

Convert Int Into Str While In __getitem__ Python 2.7

Okay, i have this string: string = 'HelloWorld' And for this example, I am using a dictionary similar to this: dic = [{'a':'k','b':'i'},{'a':'i','b':'l'},{'a':'x','b':'n'},{'a':'q

Solution 1:

Here's my best crystal ball guess at what you want. The keys of your dictionary are only a and b, which aren't any characters in HelloWorld, 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:

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))

Output:

abcdefghij

Post a Comment for "Convert Int Into Str While In __getitem__ Python 2.7"