[Tutor] Can you modify every nth item in a list with a single assignment?

Jeff Shannon jeff@ccvcorp.com
Mon Jun 16 19:53:39 2003


R. Alan Monroe wrote:

>>R. Alan Monroe wrote:
>>    
>>
>>>I'd like to, for instance, draw a column of pixels in a 2d video
>>>buffer that's implemented as a 1d array.
>>>      
>>>
>>Any particularly compelling reason to implement this as a 1d array?
>>    
>>
>
>You may have missed some of my earlier emails on this... I'm calling
>python from a C dll so I can play with vis plugins for Sonique in
>Python. So I can't change that aspect of the implementation. Python
>interprets the buffer object as a long string.
>

Ah.  I'd skimmed over that thread, since I'm not familiar with Python's 
C API...

>Unless there is a way to convert from string to 2d array, then back
>again? (Ideally without copying the whole thing and copying it back -
>doing that would send framerates down the drain, I'm guessing)
>

Depending on how much processing you're doing, it may very well take 
less time to convert the buffer to a 2d Numeric array, perform your 
processing, and then convert it back, than it would take to step through 
the 1d list.  Especially considering that, in order to modify the buffer 
string, you *do* need to convert it to a list anyhow (or else every 
modification would involve creating a new, large string).  You need to 
start with a string and end with a string, but you can't modify strings, 
so you'll have to do *some* conversion somewhere, and I'll bet that 
string->list->string is not significantly faster than string->Numeric 
matrix->string, but list modifications *will* be slower and more painful 
than matrix math in Numeric.  (Not that I've ever used Numeric 
personally, but if it's good enough for Lawrence Livermore Nat'l 
Laboratory...)

Jeff Shannon
Technician/Programmer
Credit International