[TriLUG] OT-ish: Python array slicing question

Nathan Yinger via TriLUG trilug at trilug.org
Mon Dec 12 19:16:47 EST 2022


> Is it a deep copy? In other words, if the array is an array of arrays,
are all the sub-arrays copies too?

Coping with [:] is one level deep. For deep copies, there's a function in
the copy module https://docs.python.org/3/library/copy.html

~Nathan

On Mon, Dec 12, 2022 at 6:51 PM David Brain via TriLUG <trilug at trilug.org>
wrote:

> Steve,
>
> For dicts use copy() (shallow) :
> >>> a={'a':'ant','b':'bat','c':'cat'}
> >>> b=a.copy()
> >>> b['a']='antelope'
> >>> a
> {'a': 'ant', 'b': 'bat', 'c': 'cat'}
> >>> b
> {'a': 'antelope', 'b': 'bat', 'c': 'cat'}
>
> David.
>
> On Mon, Dec 12, 2022 at 5:44 PM Steve Litt via TriLUG <trilug at trilug.org>
> wrote:
>
> > Steve Litt via TriLUG said on Mon, 12 Dec 2022 17:32:36 -0500
> >
> > >What's the best way of making a copy of a string?
> >
> > Oh neva-mind, mere assignment does the job for strings:
> >
> > ===================================
> > #!/usr/bin/python3
> > st1='st1'
> > st2=st1
> > st2='st2'
> > print(st1)
> > print(st2)
> > ===================================
> >
> > Results:
> >
> > st1
> > st2
> >
> > Thanks,
> >
> > SteveT
> >
> > Steve Litt
> > Autumn 2022 featured book: Thriving in Tough Times
> > http://www.troubleshooters.com/bookstore/thrive.htm
> > --
> > This message was sent to: dbrain at gmail.com <dbrain at gmail.com>
> > To unsubscribe, send a blank message to trilug-leave at trilug.org from
> that
> > address.
> > TriLUG mailing list : https://www.trilug.org/mailman/listinfo/trilug
> > Unsubscribe or edit options on the web  :
> > https://www.trilug.org/mailman/options/trilug/dbrain%40gmail.com
> > Welcome to TriLUG: https://trilug.org/welcome
> --
> This message was sent to: nathan.yinger at gmail.com <nathan.yinger at gmail.com
> >
> To unsubscribe, send a blank message to trilug-leave at trilug.org from that
> address.
> TriLUG mailing list : https://www.trilug.org/mailman/listinfo/trilug
> Unsubscribe or edit options on the web  :
> https://www.trilug.org/mailman/options/trilug/nathan.yinger%40gmail.com
> Welcome to TriLUG: https://trilug.org/welcome


More information about the TriLUG mailing list