[Tutor] Iterating over a string: index and value

2006-02-10 Thread Carroll, Barry








I seem to recall reading somewhere that it is possible to concurrently
generate the index and value of a strings 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 = "">

 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 X:

 tuplseq.append(idx, process(ordval))





Where X is some construct using mystr.



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




Thanks in advance for your help. 



Barry

[EMAIL PROTECTED]

541-302-1107



Never trust anything that can think for itself 

if you can't see where it keeps its brain 

JK Rowling










___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Iterating over a string: index and value

2006-02-10 Thread Adam
Here's a list comprehension which does it: print [(i, ord(v)) for i, v in enumerate(abcdefg)][(0, 97), (1, 98), (2, 99), (3, 100), (4, 101), (5, 102), (6, 103)]and a for loop:
 for i, v in enumerate(abcdefg):... tuplseq.append((i, ord(v)))... tuplseq[(0, 97), (1, 98), (2, 99), (3, 100), (4, 101), (5, 102), (6, 103)]how's that?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Iterating over a string: index and value

2006-02-10 Thread Carroll, Barry
Adam,

That is super!  Just what I was looking for.  Thanks!

And whaddya know?  There it is in the Python 2.3 Library reference, section 2.1!

Regards,
 
Barry
[EMAIL PROTECTED]
541-302-1107

Never trust anything that can think for itself 
if you can't see where it keeps its brain 
JK Rowling
 

From: Adam [mailto:[EMAIL PROTECTED] 
Sent: Friday, February 10, 2006 1:42 PM
To: Carroll, Barry
Cc: tutor@python.org
Subject: Re: [Tutor] Iterating over a string: index and value

Here's a list comprehension which does it:
 print [(i, ord(v)) for i, v in enumerate(abcdefg)]
[(0, 97), (1, 98), (2, 99), (3, 100), (4, 101), (5, 102), (6, 103)]

and a for loop:

 for i, v in enumerate(abcdefg):
... tuplseq.append((i, ord(v)))
...
 tuplseq
[(0, 97), (1, 98), (2, 99), (3, 100), (4, 101), (5, 102), (6, 103)]

how's that?

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Iterating over a string: index and value

2006-02-10 Thread Alan Gauld
 generate the index and value of a string's characters in a single for
 statement.  Is this true or did imagine it?

 for i,c in enumerate('fred'): print i,c
...
0 f
1 r
2 e
3 d


Like that?

Alan G.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Iterating over a string: index and value

2006-02-10 Thread Victor Bouffier
On Fri, 2006-02-10 at 12:42 -0800, Carroll, Barry wrote:
 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.  
 

Hi Barry,

Have a look at enumerate:

 list(enumerate('abcdefghijk'))
[(0, 'a'),
 (1, 'b'),
 (2, 'c'),
 (3, 'd'),
 (4, 'e'),
 (5, 'f'),
 (6, 'g'),
 (7, 'h'),
 (8, 'i'),
 (9, 'j'),
 (10, 'k')]

You need to work on each tuple in the iterable, but the function takes
you halfway.
Hope it helps.

Victor



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor