"CJ" wrote:

>    Well, for some reason, the FOR loop is altering two of my lists. Using
> PRINT magic, I was able to narrow down the lines that were causing it.
> But the question remains: Why is it doing this? I'm sure there's a simple
> answer that I just overlooked in the manual or something.

here you create a list, and name it "grub":

> grub=[3,25,3,5,3,"a","a","BOB",3,3,45,36,26,25,"a",3,3,3,"bob","BOB",67]

here you add a new name for that list:

> grubrpt=grub

after this operation, "grubrpt" and "grub" are two names for the
same object, not two distinct objects.  in Python, variables are
names, not small "boxes" in memory than contain stuff.

you can add

    print id(grub), id(grubrpt)

to see the object identities.

required reading:

    http://effbot.org/zone/python-objects.htm
    (short version, available in english, french, and czech)

    http://starship.python.net/crew/mwh/hacks/objectthink.html
    (long version, with ascii art!)

some common patterns:

    to copy a list, use

        grubrpt = grub[:]

    or the copy module.  to build a new list from an old one, use

        new_list = []
        for item in old_list:
            ... do something ...
            new_list.append(item)

    for some cases, a list comprehension can be a nicer way to
    write that:

        new_list = [... item ... for item in old_list ...]

etc.

</F>



-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to