Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > how to duplicate array entries

Reply
Thread Tools

how to duplicate array entries

 
 
Sebastian
Guest
Posts: n/a
 
      01-11-2010
Hi there,

I have an array x=[1,2,3]

Is there an operator which I can use to get the result
[1,1,1,2,2,2,3,3,3] ?

I tried x*3, which resulted in [1,2,3,1,2,3,1,2,3]
I also tried [[b,b,b] for b in x] which led to [[1,2,3],[1,2,3],
[1,2,3]], but this isn't what I want either.

Cheers, Sebastian
 
Reply With Quote
 
 
 
 
Sebastian
Guest
Posts: n/a
 
      01-11-2010
On Jan 11, 4:21 pm, Sebastian <(E-Mail Removed)> wrote:

> I also tried [[b,b,b] for b in x] which led to [[1,2,3],[1,2,3],
> [1,2,3]]


Sorry, I have to correct myself. The quoted line above resulted in
[[1,1,1],[2,2,2],[3,3,3]] of course!

Cheers, Sebastian
 
Reply With Quote
 
 
 
 
Chris Rebert
Guest
Posts: n/a
 
      01-11-2010
On Sun, Jan 10, 2010 at 10:21 PM, Sebastian <(E-Mail Removed)> wrote:
> Hi there,
>
> I have an array *x=[1,2,3]
>
> Is there an operator which I can use to get the result
> [1,1,1,2,2,2,3,3,3] ?
>
> I tried x*3, which resulted in [1,2,3,1,2,3,1,2,3]
> I also tried [[b,b,b] for b in x] which led to [[1,2,3],[1,2,3],
> [1,2,3]], but this isn't what I want either.


from itertools import chain, repeat
n = 3
stretched = list(chain(*[repeat(item, n) for item in x]))

Cheers,
Chris
--
http://blog.rebertia.com
 
Reply With Quote
 
Paul Rudin
Guest
Posts: n/a
 
      01-11-2010
Sebastian <(E-Mail Removed)> writes:

> Hi there,
>
> I have an array x=[1,2,3]


In python such an object is called a "list".

(In cpython it's implemented as an automatically resizable array.)

>
> Is there an operator which I can use to get the result
> [1,1,1,2,2,2,3,3,3] ?


There's no operator that will give you that directly - but there are
plenty of one-liners that will yield that list.
e.g:

>>> list(itertools.chain(*([x]*3 for x in [1,2,3])))

[1, 1, 1, 2, 2, 2, 3, 3, 3]
 
Reply With Quote
 
Gary Herron
Guest
Posts: n/a
 
      01-11-2010
Paul Rudin wrote:
> Sebastian <(E-Mail Removed)> writes:
>
>
>> Hi there,
>>
>> I have an array x=[1,2,3]
>>

>
> In python such an object is called a "list".
>
> (In cpython it's implemented as an automatically resizable array.)
>
>
>> Is there an operator which I can use to get the result
>> [1,1,1,2,2,2,3,3,3] ?
>>

>
> There's no operator that will give you that directly - but there are
> plenty of one-liners that will yield that list.
> e.g:
>
>
>>>> list(itertools.chain(*([x]*3 for x in [1,2,3])))
>>>>

> [1, 1, 1, 2, 2, 2, 3, 3, 3]
>


List comprehension also works nicely for this problem, and may be
clearer to some.

>>> x = [1,2,3]
>>> print [i for i in x for k in range(3)]

[1, 1, 1, 2, 2, 2, 3, 3, 3]

Gary Herron



 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      01-11-2010
On Sun, 10 Jan 2010 22:21:54 -0800, Sebastian wrote:

> Hi there,
>
> I have an array x=[1,2,3]


You have a list. Python has an array type, but you have to "import array"
to use it.


> Is there an operator which I can use to get the result
> [1,1,1,2,2,2,3,3,3] ?


Not an operator, but you can do it easily with a function. Here's the
simple version:

>>> def duplicate(items, count):

.... L = []
.... for item in items:
.... L.extend([item]*count)
.... return L
....
>>> duplicate([1,2,3], 3)

[1, 1, 1, 2, 2, 2, 3, 3, 3]



Here's a version which is short and simple enough to use in-line, but
will be slow for large lists:


>>> x = [1,2,3]
>>> count = 3
>>> sum([[item]*count for item in x], [])

[1, 1, 1, 2, 2, 2, 3, 3, 3]


Finally, here's a nasty hack that you should never, ever, ever use for
any reason except to win a bet:


>>> [locals()['_[1]'].extend([item]*(count-1)) or item for item in x]

[1, 1, 1, 2, 2, 2, 3, 3, 3]



--
Steven
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      01-11-2010
* Paul Rudin:
> Sebastian <(E-Mail Removed)> writes:
>
>> I have an array x=[1,2,3]

>
> In python such an object is called a "list".
>
> (In cpython it's implemented as an automatically resizable array.)


I don't think the OP's terminology needs correction.

A Python "list" is an array functionality-wise.

If one isn't observant of that fact then one ends up with O(n^2) time for the
simplest things.

Using the term "array" accentuates and clarifies this most important aspect.

Using the misleading term "list", even if that's the type's name in Python,
hides this most important aspect, and so is not, IMHO, a Good Idea except where
it really matters that it's a 'list' array as opposed to, say, a 'tuple' array.


>> Is there an operator which I can use to get the result
>> [1,1,1,2,2,2,3,3,3] ?

>
> There's no operator that will give you that directly - but there are
> plenty of one-liners that will yield that list.
> e.g:
>
>>>> list(itertools.chain(*([x]*3 for x in [1,2,3])))

> [1, 1, 1, 2, 2, 2, 3, 3, 3]


And I think it's worth noting that, for the general case, this notation is also
hiding a gross inefficiency, first constructing sub-arrays and then copying them
and joining them.

It doesn't even buy clarity.

So, just

>>> def repeat_items_in( s, n ):

.... a = []
.... for item in s:
.... for i in range( n ):
.... a.append( item )
.... return a
....
>>> repeat_items_in( [1, 2, 3], 3 )

[1, 1, 1, 2, 2, 2, 3, 3, 3]
>>> _


And if one absolutely feels like trading some efficiency and clarity for some
more functional-programming expression like thing (I don't understand why people
desire that!), just replace the 'append' line with a 'yield' and then write

list( repeat_items_in( [1, 2, 3], 3 ) )

Re the thing I don't understand: it's the same in C++, people using hours on
figuring out how to do something very simple in an ungrokkable indirect and
"compiled" way using template metaprogramming stuff, when they could just write
a simple 'for' loop and be done with in, say, 3 seconds, and much clearer too!


Cheers,

- Alf
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      01-11-2010
On Mon, 11 Jan 2010 08:56:36 +0100, Alf P. Steinbach wrote:

> * Paul Rudin:
>> Sebastian <(E-Mail Removed)> writes:
>>
>>> I have an array x=[1,2,3]

>>
>> In python such an object is called a "list".
>>
>> (In cpython it's implemented as an automatically resizable array.)

>
> I don't think the OP's terminology needs correction.
>
> A Python "list" is an array functionality-wise.
>
> If one isn't observant of that fact then one ends up with O(n^2) time
> for the simplest things.


Well that's certainly not true. Some operations may be O(N**2), but
others are not: list.append() is amortized O(N) and for individual
appends, may be can be as fast as O(1).


> Using the term "array" accentuates and clarifies this most important
> aspect.


But Python lists are designed to behave as lists. Just because CPython
implements them using arrays doesn't make them arrays. Other Python
implementations might use other implementations...

If the creator of CLPython is out there, perhaps might like to comment on
whether he uses Lisp linked-lists for the Python list type?


> Using the misleading term "list", even if that's the type's name in
> Python, hides this most important aspect, and so is not, IMHO, a Good
> Idea except where it really matters that it's a 'list' array as opposed
> to, say, a 'tuple' array.


Or an "array" array.


>>> from array import array
>>> array

<type 'array.array'>



>>> Is there an operator which I can use to get the result
>>> [1,1,1,2,2,2,3,3,3] ?

>>
>> There's no operator that will give you that directly - but there are
>> plenty of one-liners that will yield that list. e.g:
>>
>>>>> list(itertools.chain(*([x]*3 for x in [1,2,3])))

>> [1, 1, 1, 2, 2, 2, 3, 3, 3]

>
> And I think it's worth noting that, for the general case, this notation
> is also hiding a gross inefficiency, first constructing sub-arrays and
> then copying them and joining them.


I wouldn't call that a gross inefficiency -- that's a gross exaggeration
unless count is very large, and even then, possibly not that large. Only
one such sub-array (sub-list) exists at any time. (Unless I am grossly
misinformed.)



> It doesn't even buy clarity.


Not if you're unused to the functional, iterator-based idioms, no.

But if you are, it does.



> And if one absolutely feels like trading some efficiency and clarity for
> some more functional-programming expression like thing (I don't
> understand why people desire that!),


I don't understand why you assume that functional forms are necessarily
less efficient and clear than non-functional. Which is easier to read?

>>> print sum([1,2,3])

6

versus

>>> total = 0
>>> for i in [1, 2, 3]:

.... total += i
....
>>> print total

6


[...]
> Re the thing I don't understand: it's the same in C++, people using
> hours on figuring out how to do something very simple in an ungrokkable
> indirect and "compiled" way using template metaprogramming stuff, when
> they could just write a simple 'for' loop and be done with in, say, 3
> seconds, and much clearer too!


Amen to that brother!

It's the obsession with one-liners and the desire for a single built-in
command to do every imaginable task.





--
Steven
 
Reply With Quote
 
Sebastian
Guest
Posts: n/a
 
      01-11-2010
Thank you for your answers! I actually implemented it using for loops
before I posted here, but I was curious if there is a more elegant
solution (judging from the post, Alf will probably say, that for loops
are already elegant).

Sebastian
 
Reply With Quote
 
Munir
Guest
Posts: n/a
 
      01-11-2010
> I have an array *x=[1,2,3]
>
> Is there an operator which I can use to get the result
> [1,1,1,2,2,2,3,3,3] ?
>
> I tried x*3, which resulted in [1,2,3,1,2,3,1,2,3]


Have you tried:

y = x*3
y.sort()

Munir
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Picking X random entries from linked list of Y entries Don Bruder C Programming 3 08-03-2010 09:10 AM
Windows service making duplicate event log entries. Wade ASP .Net 3 07-19-2005 12:53 PM
Tying up Port Login table entries with Port Table Entries in CISCO SNMP John Ramsden Cisco 0 07-24-2004 04:03 PM
Eliminating duplicate entries A.T. XML 1 07-22-2004 10:54 PM



Advertisments