Problem with a dictionary program....

Jeffrey Froman jeffrey at fro.man
Tue Sep 28 16:47:11 CEST 2004


Ling Lee wrote:

> After I have gotten the lenght of the string, I will write a loop, that
> goes through the dictionary as many times as the lengt of the string, and
> the gives me the corresponding numbers, the numner 21 would go 2 times
> through the loop and give me the output two one.

There is no need to count the length. You can iterate over each character in
a Python string (or other object) without first calculating the size of the
loop, like so:

output = []
for character in indput:
 output.append(List[character])
print ', '.join(output)

As Russel pointed out, you'll have to iterate over indput as as a string --
not convert it to an integer first, because you can't iterate over an
integer's digits, but you can iterate over a string's characters.

Jeffrey



More information about the Python-list mailing list