Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #65377 > unrolled thread

[newbie] copying identical list for a function argument

Started byJean Dupont <jeandupont314@gmail.com>
First post2014-02-03 13:36 -0800
Last post2014-02-03 19:09 -0800
Articles 6 — 6 participants

Back to article view | Back to comp.lang.python


Contents

  [newbie] copying identical list for a function argument Jean Dupont <jeandupont314@gmail.com> - 2014-02-03 13:36 -0800
    Re: [newbie] copying identical list for a function argument Gary Herron <gary.herron@islandtraining.com> - 2014-02-03 14:06 -0800
    Re: [newbie] copying identical list for a function argument Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-02-03 22:19 +0000
      Re: [newbie] copying identical list for a function argument Jean Dupont <jeandupont115@gmail.com> - 2014-02-04 01:14 -0800
    Re: [newbie] copying identical list for a function argument Tim Chase <python.list@tim.thechases.com> - 2014-02-03 15:51 -0600
    Re: [newbie] copying identical list for a function argument Rustom Mody <rustompmody@gmail.com> - 2014-02-03 19:09 -0800

#65377 — [newbie] copying identical list for a function argument

FromJean Dupont <jeandupont314@gmail.com>
Date2014-02-03 13:36 -0800
Subject[newbie] copying identical list for a function argument
Message-ID<df3d41dd-222d-4a05-b9de-377d67b77bda@googlegroups.com>
I have a list like this:
[1,2,3]

The argument of my function should be a repeated version e.g.
[1,2,3],[1,2,3],[1,2,3],[1,2,3] (could be a different number of times repeated also)

what is the prefered method to realize this in Python?

any help would be really appreciated

kind regards,
jean

[toc] | [next] | [standalone]


#65381

FromGary Herron <gary.herron@islandtraining.com>
Date2014-02-03 14:06 -0800
Message-ID<mailman.6367.1391465505.18130.python-list@python.org>
In reply to#65377
On 02/03/2014 01:36 PM, Jean Dupont wrote:
> I have a list like this:
> [1,2,3]
>
> The argument of my function should be a repeated version e.g.
> [1,2,3],[1,2,3],[1,2,3],[1,2,3] (could be a different number of times repeated also)

That's not very clear.  You say "The" argument (singular; meaning 1) but 
then show what seems to be four arguments.  Can you show us the function 
you mention.

A number of things come to mind:

 >>> 4*[[1,2,3]]
[[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]

so fn(n*[[1,2,3]]) might do for a single parameter function
   def fn(L):
       ...

On the other hand expanding a sequence into individual parameters:
     fn(*(4*[[1,2,3]]))  # note extra * preceding the arg
willl be a valid call for
   def fn(a,b,c,d):
     ...

I'm sure other interpretations of your question are possible.

Gary Herron




>
> what is the prefered method to realize this in Python?
>
> any help would be really appreciated
>
> kind regards,
> jean
>

[toc] | [prev] | [next] | [standalone]


#65382

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2014-02-03 22:19 +0000
Message-ID<52f015fa$0$29972$c3e8da3$5496439d@news.astraweb.com>
In reply to#65377
On Mon, 03 Feb 2014 13:36:24 -0800, Jean Dupont wrote:

> I have a list like this:
> [1,2,3]
> 
> The argument of my function should be a repeated version e.g.
> [1,2,3],[1,2,3],[1,2,3],[1,2,3] (could be a different number of times
> repeated also)
> 
> what is the prefered method to realize this in Python?


I don't really understand your question. It could mean any of various 
things, so I'm going to try to guess what you mean. If my guesses are 
wrong, please ask again, giving more detail, and possibly an example of 
what you want to do and the result you expect.

I think you mean that you have some function that needs to take (say) 
five arguments, and you want to avoid writing:

result = function([1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 23], [1, 2, 3])

because it's too easy to make a mistake (as I did, deliberately, above -- 
can you see it?).

If my guess is correct, try this:

mylist = [1, 2, 3]
result = function(mylist, mylist, mylist, mylist, mylist)


That's perfectly reasonable for two or three arguments, but not so much 
for five. Instead, here's a trick: first we make five identical 
references to the same list:

[mylist]*5  # same as [mylist, mylist, mylist, mylist, mylist]

then expand them as arguments to the function:

mylist = [1, 2, 3]
list_of_lists = [mylist]*5
result = function(*list_of_lists)

(The * operator means multiplication when used between two arguments, and 
inside a function call a leading * also does argument expansion.)


But wait... there's something slightly weird here. Even though there are 
five distinct references to mylist, they're all the same list! Change 
one, change all. This may be what you want, or it may be a problem. Hard 
to tell from your question.

Think about references as being a little bit like names. A *single* 
person could be known as "son", "Dad", "Mr Obama", "Barack", "Mr 
President", "POTUS", and more. In this case, we have a single list, [1, 
2, 3], which is known by six references: the name "mylist", and five 
additional references list_of_lists index 0, list_of_lists index 1, and 
so on up to list_of_lists index 4.

We can prove that they all refer to the same list by running a bit of 
code in the interactive interpreter:


py> mylist = [1, 2, 3]
py> list_of_lists = [mylist]*5
py> list_of_lists
[[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]
py> mylist.append(99)
py> list_of_lists
[[1, 2, 3, 99], [1, 2, 3, 99], [1, 2, 3, 99], [1, 2, 3, 99], [1, 2, 3, 
99]]


So rather than having five references to the same, identical, list, you 
might want five *copies*. You can copy a list using slicing:

mylist = [1, 2, 3]
copy = mylist[:]

Instead of using list multiplication to repeat five identical lists, we 
make five copies using a list comprehension:

list_of_lists = [mylist[:] for i in range(5)]

then expand it in the function call as before:

result = function(*list_of_lists)


Hope this helps,



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#65411

FromJean Dupont <jeandupont115@gmail.com>
Date2014-02-04 01:14 -0800
Message-ID<76a5550d-fef4-48bd-aa36-fc0af75db104@googlegroups.com>
In reply to#65382
Op maandag 3 februari 2014 23:19:39 UTC+1 schreef Steven D'Aprano:
> On Mon, 03 Feb 2014 13:36:24 -0800, Jean Dupont wrote:
> > I have a list like this:
> > [1,2,3]
> > 
> > The argument of my function should be a repeated version e.g.
> > [1,2,3],[1,2,3],[1,2,3],[1,2,3] (could be a different number of times
> > repeated also)
> > 
> > what is the prefered method to realize this in Python?
>
> I don't really understand your question. It could mean any of various 
> things, so I'm going to try to guess what you mean. If my guesses are 
> wrong, please ask again, giving more detail, and possibly an example of 
> what you want to do and the result you expect.
> I think you mean that you have some function that needs to take (say) 
> five arguments, and you want to avoid writing:
> result = function([1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 23], [1, 2, 3])
> because it's too easy to make a mistake (as I did, deliberately, above -- 
> can you see it?).
> If my guess is correct, try this:
> mylist = [1, 2, 3]
> result = function(mylist, mylist, mylist, mylist, mylist)
>
> That's perfectly reasonable for two or three arguments, but not so much 
> for five. Instead, here's a trick: first we make five identical 
> references to the same list:
> [mylist]*5  # same as [mylist, mylist, mylist, mylist, mylist]
> then expand them as arguments to the function:
> mylist = [1, 2, 3]
> list_of_lists = [mylist]*5
> result = function(*list_of_lists)
> (The * operator means multiplication when used between two arguments, and 
> inside a function call a leading * also does argument expansion.)
>
> But wait... there's something slightly weird here. Even though there are 
> five distinct references to mylist, they're all the same list! Change 
> one, change all. This may be what you want, or it may be a problem. Hard 
> to tell from your question.
> Think about references as being a little bit like names. A *single* 
> person could be known as "son", "Dad", "Mr Obama", "Barack", "Mr 
> President", "POTUS", and more. In this case, we have a single list, [1, 
> 2, 3], which is known by six references: the name "mylist", and five 
> additional references list_of_lists index 0, list_of_lists index 1, and 
> so on up to list_of_lists index 4.
> We can prove that they all refer to the same list by running a bit of 
> code in the interactive interpreter:
>
> py> mylist = [1, 2, 3]
> py> list_of_lists = [mylist]*5
> py> list_of_lists
> [[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]
> py> mylist.append(99)
> py> list_of_lists
> [[1, 2, 3, 99], [1, 2, 3, 99], [1, 2, 3, 99], [1, 2, 3, 99], [1, 2, 3, 
> 99]]
>
> So rather than having five references to the same, identical, list, you 
> might want five *copies*. You can copy a list using slicing:
> mylist = [1, 2, 3]
> copy = mylist[:]
> Instead of using list multiplication to repeat five identical lists, we 
> make five copies using a list comprehension:
> list_of_lists = [mylist[:] for i in range(5)]
> then expand it in the function call as before:
> result = function(*list_of_lists)
>
> Hope this helps,
Yes it does, thanks a lot to you and all the others who responded, "the
missing link" which until now I wasn't aware of but which was essential
for the solution was the "*" in
result = function(*list_of_lists)

kind regards,
jean

[toc] | [prev] | [next] | [standalone]


#65386

FromTim Chase <python.list@tim.thechases.com>
Date2014-02-03 15:51 -0600
Message-ID<mailman.6370.1391468667.18130.python-list@python.org>
In reply to#65377
On 2014-02-03 13:36, Jean Dupont wrote:
> I have a list like this:
> [1,2,3]
> 
> The argument of my function should be a repeated version e.g.
> [1,2,3],[1,2,3],[1,2,3],[1,2,3] (could be a different number of
> times repeated also)
> 
> what is the prefered method to realize this in Python?
> 
> any help would be really appreciated

It depends on whether you want the contained list to be the *same*
list or *copies* of that list.  You can do either of the following:

  lst = [1,2,3]
  dupes1 = [lst[:] for _ in range(5)]
  dupes2 = [lst for _ in range(5)]
  dupes3 = [lst] * 5

The dupes2 and dupes3 should be the same (the latter is a simpler
syntax for it): each contains the same list multiple times.  To see
this, do

  lst.append(4)

and then inspect dupes1 compared to dupes2/dupes3.

-tkc

[toc] | [prev] | [next] | [standalone]


#65400

FromRustom Mody <rustompmody@gmail.com>
Date2014-02-03 19:09 -0800
Message-ID<25b4ba64-6d8a-4006-ade5-f47561fdfa6a@googlegroups.com>
In reply to#65377
On Tuesday, February 4, 2014 3:06:24 AM UTC+5:30, Jean Dupont wrote:
> I have a list like this:
> [1,2,3]

> The argument of my function should be a repeated version e.g.
> [1,2,3],[1,2,3],[1,2,3],[1,2,3] (could be a different number of times repeated also)

> what is the prefered method to realize this in Python?

Probably not what you are asking... But maybe you want to
look at repeat (and other stuff) in itertools


>>> from itertools import repeat

>>> zip(range(0,10), repeat("hi"))
[(0, 'hi'), (1, 'hi'), (2, 'hi'), (3, 'hi'), (4, 'hi'), (5, 'hi'), (6, 'hi'),
 (7, 'hi'), (8, 'hi'), (9, 'hi')]
 

IOW repeat give you "indefinite-data-repeat" just as "while True:..."
gives "indefinite-code-repeat"

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web