Python equivalent to *nix 'banner' problem

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Wed Oct 24 01:26:29 EDT 2007


En Tue, 23 Oct 2007 20:29:53 -0300, Looney, James B  
<james.b.looney at lmco.com> escribió:

> Regardless of whether I run this from IRIX or Windows, I get the same
> results.  I'm expecting the string "ab" to be displayed twice.  Instead,
> the second time, I get ABB.  If I add a 3rd print line, I'll get ABBB.
> I'm now stuck trying to understand what's going on.  It seems like
> there's an overflow somewhere since it's overwriting my dictionary for
> the letter 'a', but I don't know how it's doing it.

> ########
>    def getBanner( self, inputStr ):
>       retVal = []
>       for c in inputStr:
>          banneredCharList = self.myBannerDict[ c ]
>          if( 0 == len( retVal ) ):
>             retVal = banneredCharList
>          else:
>             for i in range( len( banneredCharList ) ):
>                retVal[ i ] += banneredCharList[ i ]

banneredCharList is a list, taken from myBannerDict.
retval = banneredCharList does not make a copy, just points the name  
retval to the list.
Later, retval[i] += ... is modifying the list, which is always the same,  
so you are modifying myBannerDict actually.

Try using: retVal = banneredCharList[:]


-- 
Gabriel Genellina




More information about the Python-list mailing list