Strings and Lists

Donn Cave donn at u.washington.edu
Mon Apr 18 12:02:29 EDT 2005


In article <d769864e.0504180342.426357d3 at posting.google.com>,
 tomlongridge at gmail.com (Tom Longridge) wrote:

> My current Python project involves lots repeatating code blocks,
> mainly centred around a binary string of data. It's a genetic
> algorithm in which there are lots of strings (the chromosomes) which
> get mixed, mutated and compared a lot.
> 
> Given Python's great list processing abilities and the relative
> inefficiencies some string operations, I was considering using a list
> of True and False values rather than a binary string.
> 
> I somehow doubt there would be a clear-cut answer to this, but from
> this description, does anyone have any reason to think that one way
> would be much more efficient than the other? (I realise the best way
> would be to do both and `timeit` to see which is faster, but it's a
> sizeable program and if anybody considers it a no-brainer I'd much
> rather know now!)

I make no representations about how much mileage you would
get out of it, but if character data suits your purposes and
you just need a mutable array - in principle, I think it would
be more efficient to use an array.  Like,
   import array
   a = array.array('c', strdata)

As I understand it, this object simply contains character data,
not a list of 1 character string objects, so it's much more
economical to store and examine.

   Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list