[Tutor] Iterating over a string: index and value

Carroll, Barry Barry.Carroll at psc.com
Fri Feb 10 21:42:58 CET 2006


I seem to recall reading somewhere that it is possible to concurrently
generate the index and value of a string's characters in a single for
statement.  Is this true or did imagine it?

 

Here is the scenario:

 

Given an ASCII string of arbitrary length and content, generate a
sequence of tuples whose elements are: 

    the index of each character in the string, and 

    data based on the ordinal value of the character in the ASCII
collating sequence.  

 

The brute force way to do this is 

 

>>>>>>>>>> 

tuplseq = ()

for idx in mystr:

    char = mystr[idx]

    ordval = ord(char)

    data = process(ordval)

    tuplseq.append(idx, data)

>>>>>>>>>> 

 

Is there a way to generate the character (or its ord value) along with
the index?  E.g.:

 

>>>>>>>>>> 

tuplseq = ()

for idx ordval in XXXXXXXXX:

    tuplseq.append(idx, process(ordval))

>>>>>>>>>> 

 

Where XXXXXXXXX is some construct using mystr.

 

BTW, this is for internal software for our test group.  

 

Thanks in advance for your help.  

 

Barry

barry.carroll at psc.com

541-302-1107

________________________

"Never trust anything that can think for itself 

if you can't see where it keeps its brain" 

JK Rowling

 

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060210/9a67a808/attachment-0001.htm 


More information about the Tutor mailing list