Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Idiomatic way of repeating items in a sequence.

Reply
Thread Tools

Idiomatic way of repeating items in a sequence.

 
 
Jeff Epler
Guest
Posts: n/a
 
      06-30-2003
Here's one:

def repeatitems(sequence, repetitions):
r = [None] * repetitions
return [i for i in sequence for j in r]

Here's another, using generator functions (so the return is an iterator,
not a list):

def repeatitems(sequence, repetitions):
r = [None] * repetitions
for item in sequence:
for i in r:
yield item

In both cases I've performed an "optimization" by precomputing a list
with len(repetitions) instead of computing it once for each item in
sequence. Whether this makes a difference, I don't know.

Jeff

 
Reply With Quote
 
 
 
 
Mike C. Fletcher
Guest
Posts: n/a
 
      06-30-2003
John Hunter wrote:

>This doesn't look too bad to me, but perhaps list comprehensions are
>clearer?
>
> seq = ['a', 'b', 'c']
> print [x for x in seq for x in seq]
>
>>> def repeat3( sequence, count=1 ):

.... return [x for x in sequence for i in range(count) ]
....
>>> repeat3( [2,3,4], 3 )

[2, 2, 2, 3, 3, 3, 4, 4, 4]

I *think* is what you were suggesting, and is indeed very clear. For
those into generators, this is fun (but has no huge advantage if you
wind up using repeat instead of irepeat, or if you're using small
sequences):

>>> from __future__ import generators
>>> def irepeat( sequence, count=1 ):

.... countSet = range(count)
.... for item in sequence:
.... for i in countSet:
.... yield item
....
>>> def repeat( sequence, count = 1 ):

.... return list(irepeat(sequence, count))
....
>>> repeat( [2,3,4], 3 )

[2, 2, 2, 3, 3, 3, 4, 4, 4]

Enjoy,
Mike

_______________________________________
Mike C. Fletcher
Designer, VR Plumber, Coder
http://members.rogers.com/mcfletch/




 
Reply With Quote
 
 
 
 
alr
Guest
Posts: n/a
 
      07-02-2003
Wow, tanks for alle the replies. My favourite is John Hunters/Bob
Gailers solution ([x for x in seq for i in range(repetitions)]). I had
forgotten that you could have nested for statements in list
literals... Aahz's point is taken. I happen to need to repeat lists of
strings (which are immutable), but that's not what i asked about now
is it.

--
Regards
André Risnes
 
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
idiomatic way to assign if not nil? Jay Levitt Ruby 19 11-29-2010 03:23 PM
A better idiomatic way of doing this?! Tim Romberg Ruby 10 09-06-2010 07:38 PM
idiomatic way to collect and report multiple exceptions? Ben Cohen Python 4 05-10-2010 09:50 AM
Idiomatic way to detect first/last iteration? Kendall Gifford Ruby 18 06-12-2009 02:25 PM
Idiomatic way for collecting elements using REXML? John Lam Ruby 4 05-29-2005 12:28 PM



Advertisments