Problems with copies (and references) to lists
Michael Gilfix
mgilfix at eecs.tufts.edu
Wed Apr 17 23:55:30 EDT 2002
I found this a little misleading. What's 's' have to do with
anything? Here's a couple of comments:
* Check out 'import copy' and use that to copy lists.
* Try using map to apply functions to various elements
without any iteration. That might help some.
-- Mike
On Wed, Apr 17 @ 17:12, bvdpoel at uniserve.com wrote:
>
> Took awhile, but I do have the following bit of code working as it
> should. The doc string is misleading, but I'm not sure of how better to
> describe it out of context... The important thing is, it is giving the
> right result, and it is NOT changing the original list.
>
> -------
> def doubleUp(a, fact):
> """ Makes fact/2 copies of list [ [s, l, v,...] ].
> New copy has all values of l divided by fact/2 and
> copies of s set to s/2 and 50+(s/2).
> fact will always be a power of 2.
> """
>
> new = a
> while fact >1:
> b1 = []
> b2 = []
> for t in new[:]:
> n=t[:] # this prevents munging our
> original
> n[0] /= 2
> n[1] /= 2
> b1+=[n[:]] # [:] forces new copy of n!
> n[0] += 50
> b2+=[n]
> fact /=2
> new = b1 + b2
> return new
>
> a = [[0.0, 20.0, 66, 44],[50.0, 20.0, 55, 33]]
> print doubleUp(a, 2)
> print doubleUp(a,4)
> print a
>
> --------
>
> But, I'm wondering if it is at all efficient. Or even good python. Seems
> that the number of [:] needed just makes for ugly code.
>
> --
> Bob van der Poel ** Wynndel, British Columbia, CANADA **
> EMAIL: bvdpoel at uniserve.com
> WWW: http://users.uniserve.com/~bvdpoel
>
> --
> http://mail.python.org/mailman/listinfo/python-list
`-> (bvdpoel)
--
Michael Gilfix
mgilfix at eecs.tufts.edu
For my gpg public key:
http://www.eecs.tufts.edu/~mgilfix/contact.html
More information about the Python-list
mailing list