how to append to a list twice?
If I want to create a list of the form [100, 99, 99, 98, 98, 97, 97...] (where each item is repeated twice after the first one), how might I do that most efficiently? Right now I have this: series = [100] for x in range(10): # just for testing series.append(series[-1] - 1) But of course that only does it once, and I don't want to have to copy and paste the append line. Perhaps there's a better way than this. Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Re: how to append to a list twice?
"John Salerno" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > If I want to create a list of the form [100, 99, 99, 98, 98, 97, 97...] > (where each item is repeated twice after the first one), how might I do > that most efficiently? > > Right now I have this: > > series = [100] > for x in range(10): # just for testing > series.append(series[-1] - 1) > > But of course that only does it once, and I don't want to have to copy > and paste the append line. Perhaps there's a better way than this. > > Thanks. series = [100] for x in range(10): # just for testing series.extend([series[-1] - 1]*2) -- http://mail.python.org/mailman/listinfo/python-list
Re: how to append to a list twice?
John Salerno wrote: > If I want to create a list of the form [100, 99, 99, 98, 98, 97, 97...] > (where each item is repeated twice after the first one), how might I do > that most efficiently? > > Right now I have this: > > series = [100] > for x in range(10): # just for testing > series.append(series[-1] - 1) > > But of course that only does it once, and I don't want to have to copy > and paste the append line. Perhaps there's a better way than this. > > Thanks. series = [100] for i in range(1,10): series.extend([100-i]*2) print series [100, 99, 99, 98, 98, 97, 97, 96, 96, 95, 95, 94, 94, 93, 93, 92, 92, 91, 91] Gerard -- http://mail.python.org/mailman/listinfo/python-list
Re: how to append to a list twice?
John Salerno <[EMAIL PROTECTED]> wrote: > If I want to create a list of the form [100, 99, 99, 98, 98, 97, 97...] > (where each item is repeated twice after the first one), how might I do > that most efficiently? > > Right now I have this: > > series = [100] > for x in range(10): # just for testing > series.append(series[-1] - 1) > > But of course that only does it once, and I don't want to have to copy > and paste the append line. Perhaps there's a better way than this. def makeseries(N): series = [N] append = series.append for tailer in xrange(N-1, -1, -1): append(tailer) append(tailer) return series series = makeseries(100) assuming that by "most efficiently" you mean "fastest", this might come close; you'll want to also time an alternative where makeseries is a generator which just yields the values, and the last assignment becomes series=list(makeseries(100)). Alex -- http://mail.python.org/mailman/listinfo/python-list
Re: how to append to a list twice?
> If I want to create a list of the form [100, 99, 99, 98, 98, 97, 97...] > (where each item is repeated twice after the first one), how might I do > that most efficiently? > > Right now I have this: > > series = [100] > for x in range(10): # just for testing > series.append(series[-1] - 1) Well, you can do something like def foo(start): count = 0 while 1: yield start - ((count + 1) / 2) count += 1 which is pretty efficient in the matter. If you just need the results, you can do something like series = [100] for _ in range(10): series.extend([x[-1]-1]*2) Both should get you what you want, and one or the other may be better depending on the scenario in which you want to use it. -tkc -- http://mail.python.org/mailman/listinfo/python-list
Re: how to append to a list twice?
Not sure if this is simpler or better, but here's a way to do it with a generator: value = 100 count = 0 def valueGen(): global value global count while(value >= 0): if(value == 100): yield value value -= 1 else: if(count & 1): yield value else: yield value value -= 1 count = count + 1 series = [] gen= valueGen() for item in gen: print item series.append(item) print series -- http://mail.python.org/mailman/listinfo/python-list
Re: how to append to a list twice?
Paul McGuire wrote: > "John Salerno" <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED] >> If I want to create a list of the form [100, 99, 99, 98, 98, 97, 97...] >> (where each item is repeated twice after the first one), how might I do >> that most efficiently? >> >> Right now I have this: >> >> series = [100] >> for x in range(10): # just for testing >> series.append(series[-1] - 1) >> >> But of course that only does it once, and I don't want to have to copy >> and paste the append line. Perhaps there's a better way than this. >> >> Thanks. > > series = [100] > for x in range(10): # just for testing > series.extend([series[-1] - 1]*2) > > Interesting. I tried the *2 method twice, but I kept getting weird results, I guess because I was using append and not extend. I thought extend added lists to lists, but obviously that's not the case here. Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Re: how to append to a list twice?
Alex Martelli wrote: > > But of course that only does it once, and I don't want to have to copy > > and paste the append line. Perhaps there's a better way than this. > > def makeseries(N): > series = [N] > append = series.append > for tailer in xrange(N-1, -1, -1): > append(tailer) > append(tailer) But Now You've Violated The DRY Principle!!! -- http://mail.python.org/mailman/listinfo/python-list
Re: how to append to a list twice?
> Interesting. I tried the *2 method twice, but I kept > getting weird results, I guess because I was using append > and not extend. I thought extend added lists to lists, > but obviously that's not the case here. In the above example, it *is* "add[ing] lists to lists". Note the set of brackets: series = [100] for x in range(10): # just for testing series.extend([series[-1] - 1]*2) You have a one-element series: [series[-1] - 1] that gets duplicated using the overloading of the multiplication ("duplication") operator: [...] * 2 This yields a two-element list. This list then gets passed to extend(), to add those two elements to the original list. If you used append() instead of extend(), it would be something like [100, [99, 99], [98, 98],...] -tkc -- http://mail.python.org/mailman/listinfo/python-list
Re: how to append to a list twice?
Gerard Flanagan wrote: > John Salerno wrote: > > If I want to create a list of the form [100, 99, 99, 98, 98, 97, 97...] > > (where each item is repeated twice after the first one), how might I do > > that most efficiently? > > series = [100] > > for i in range(1,10): > series.extend([100-i]*2) > > print series > > [100, 99, 99, 98, 98, 97, 97, 96, 96, 95, 95, 94, 94, 93, 93, 92, 92, > 91, 91] Alternative: - series = [100] r = xrange(99,90,-1) for L in ( [a1,a2] for a1,a2 in zip( r, r ) ): series.extend(L) print series out: [100, 99, 99, 98, 98, 97, 97, 96, 96, 95, 95, 94, 94, 93, 93, 92, 92, 91, 91] - series = ['a'] r = 'bcdefgh' for L in ( [a1,a2] for a1,a2 in zip( r, r ) ): series.extend(L) print series out: ['a', 'b', 'b', 'c', 'c', 'd', 'd', 'e', 'e', 'f', 'f', 'g', 'g', 'h', 'h'] - Gerard -- http://mail.python.org/mailman/listinfo/python-list
Re: how to append to a list twice?
John Salerno enlightened us with: > Interesting. I tried the *2 method twice, but I kept getting weird > results, I guess because I was using append and not extend. I > thought extend added lists to lists, but obviously that's not the > case here. [100].extend([90]) -> [100, 90] [100].append([90]) -> [100, [90]] [100].append(90) -> [100, 90] Sybren -- The problem with the world is stupidity. Not saying there should be a capital punishment for stupidity, but why don't we just take the safety labels off of everything and let the problem solve itself? Frank Zappa -- http://mail.python.org/mailman/listinfo/python-list
Re: how to append to a list twice?
John Salerno <[EMAIL PROTECTED]> wrote: >If I want to create a list of the form [100, 99, 99, 98, 98, 97, 97...] >(where each item is repeated twice after the first one), how might I do >that most efficiently? > >Right now I have this: > >series = [100] >for x in range(10): # just for testing > series.append(series[-1] - 1) > >But of course that only does it once, [ ... ] You could try: series = [100] l = range(series[-1]-1, series[-1]-11, -1) #just for testing s.extend(sum(zip(l, l), ())) Times as faster than the series.extend([series[-1] - 1] * 2) for me. -- \S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/ ___ | "Frankly I have no feelings towards penguins one way or the other" \X/ |-- Arthur C. Clarke her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump -- http://mail.python.org/mailman/listinfo/python-list
Re: how to append to a list twice?
John Salerno wrote: > If I want to create a list of the form [100, 99, 99, 98, 98, 97, 97...] > (where each item is repeated twice after the first one), how might I do > that most efficiently? Why not just this: series = [x/2 for x in range(200, 1, -1)] -- http://mail.python.org/mailman/listinfo/python-list
Re: how to append to a list twice?
Gerard Flanagan wrote: > Gerard Flanagan wrote: > > John Salerno wrote: > > > If I want to create a list of the form [100, 99, 99, 98, 98, 97, 97...] > > > (where each item is repeated twice after the first one), how might I do > > > that most efficiently? > > > > series = [100] > > > > for i in range(1,10): > > series.extend([100-i]*2) > > > > print series > > > > [100, 99, 99, 98, 98, 97, 97, 96, 96, 95, 95, 94, 94, 93, 93, 92, 92, > > 91, 91] > > Alternative: > > - > series = [100] > > r = xrange(99,90,-1) > > for L in ( [a1,a2] for a1,a2 in zip( r, r ) ): > series.extend(L) > > print series > > out: [100, 99, 99, 98, 98, 97, 97, 96, 96, 95, 95, 94, 94, 93, 93, 92, > 92, 91, 91] > > - > series = ['a'] > > r = 'bcdefgh' > > for L in ( [a1,a2] for a1,a2 in zip( r, r ) ): > series.extend(L) > > print series > > out: ['a', 'b', 'b', 'c', 'c', 'd', 'd', 'e', 'e', 'f', 'f', 'g', 'g', > 'h', 'h'] > - > (Nothing better to do!) def multiplier( iterable, n ): for t in zip( *[iterable] * n): yield t series1 = [100] rng = xrange(99,90,-1) for L in multiplier( rng, 2 ): series1.extend(L) series2 = ['a'] rng = 'bcdefgh' for L in multiplier( rng, 2 ): series2.extend(L) print series1 print series2 Gerard -- http://mail.python.org/mailman/listinfo/python-list
Re: how to append to a list twice?
Edward Elliott wrote: > John Salerno wrote: > > If I want to create a list of the form [100, 99, 99, 98, 98, 97, 97...] > > (where each item is repeated twice after the first one), how might I do > > that most efficiently? > > Why not just this: > > series = [x/2 for x in range(200, 1, -1)] clever! :) -- http://mail.python.org/mailman/listinfo/python-list
Re: how to append to a list twice?
John Salerno wrote: > If I want to create a list of the form [100, 99, 99, 98, 98, 97, 97...] > (where each item is repeated twice after the first one), how might I do > that most efficiently? > > Right now I have this: > > series = [100] > for x in range(10): # just for testing > series.append(series[-1] - 1) > > But of course that only does it once, and I don't want to have to copy > and paste the append line. Perhaps there's a better way than this. > > Thanks. I know this doesn't beat Edward Elliot's answer, but: py> import operator py> [100] + reduce(operator.add, [[i,i] for i in xrange(99,90,-1)]) [100, 99, 99, 98, 98, 97, 97, 96, 96, 95, 95, 94, 94, 93, 93, 92, 92, 91, 91] -- James Stroud UCLA-DOE Institute for Genomics and Proteomics Box 951570 Los Angeles, CA 90095 http://www.jamesstroud.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: how to append to a list twice?
Edward Elliott <[EMAIL PROTECTED]> wrote: > John Salerno wrote: > > If I want to create a list of the form [100, 99, 99, 98, 98, 97, 97...] > > (where each item is repeated twice after the first one), how might I do > > that most efficiently? > > Why not just this: > > series = [x/2 for x in range(200, 1, -1)] That should be series = [x//2 for x in range(200, 1, -1)] to be "from __future__ import division" safe -- Nick Craig-Wood <[EMAIL PROTECTED]> -- http://www.craig-wood.com/nick -- http://mail.python.org/mailman/listinfo/python-list
Re: how to append to a list twice?
Nick Craig-Wood wrote: > That should be > > series = [x//2 for x in range(200, 1, -1)] > > to be "from __future__ import division" safe from __present__ import gratitude -- http://mail.python.org/mailman/listinfo/python-list
Re: how to append to a list twice?
I don't get it (the Elliot solution)... How is it that the first value is repeated once times, and the remaining values are repeated twice times? -- http://mail.python.org/mailman/listinfo/python-list
Re: how to append to a list twice?
[EMAIL PROTECTED] wrote: > I don't get it (the Elliot solution)... How is it that the first value > is repeated once times, and the remaining values are repeated twice > times? > Because of the way division works. The first number in the range is 200, and 200/2 is 100. The next number is 199, and 199/2 is (as division works right now) 99 (99.5 rounded). Next is 198, and that halved is also 99 (this time with no remainder). Every two sets of numbers thereafter also do the same. -- http://mail.python.org/mailman/listinfo/python-list
Re: how to append to a list twice?
John Salerno wrote: > [EMAIL PROTECTED] wrote: >> I don't get it (the Elliot solution)... How is it that the first value >> is repeated once times, and the remaining values are repeated twice >> times? >> > > Because of the way division works. The first number in the range is 200, > and 200/2 is 100. The next number is 199, and 199/2 is (as division > works right now) 99 (99.5 rounded). Next is 198, and that halved is also > 99 (this time with no remainder). Every two sets of numbers thereafter > also do the same. I should also say, to answer your question more directly, that the reason it doesn't happen to the first number is because it only works on an odd number and the number below it, i.e. 199 and 198 both yield 99. If our range had started with 201, then 100 would have been repeated twice. -- http://mail.python.org/mailman/listinfo/python-list
Re: how to append to a list twice?
On 21 Apr 2006 12:50:38 -0700 in comp.lang.python, [EMAIL PROTECTED] wrote: >I don't get it (the Elliot solution)... How is it that the first value >is repeated once times, and the remaining values are repeated twice >times? Integer division truncates. 200/2 -> 100, 199/2 -> 99, 198/2 -> 99, etc. Regards, -=Dave -- Change is inevitable, progress is not. -- http://mail.python.org/mailman/listinfo/python-list
Re: how to append to a list twice?
John Salerno wrote: > If I want to create a list of the form [100, 99, 99, 98, 98, 97, 97...] > (where each item is repeated twice after the first one), how might I do > that most efficiently? > > Right now I have this: > > series = [100] > for x in range(10): # just for testing > series.append(series[-1] - 1) > > But of course that only does it once, and I don't want to have to copy > and paste the append line. Perhaps there's a better way than this. > > Thanks. series = [100] + [ t for t in range(99,90,-1) for _ in [0,1] ] (what's wrong with two appends again...?) -- http://mail.python.org/mailman/listinfo/python-list
Re: how to append to a list twice?
John Salerno wrote: > If I want to create a list of the form [100, 99, 99, 98, 98, 97, 97...] > (where each item is repeated twice after the first one), how might I do > that most efficiently? series = [100]*21 series[1::2] = series[2::2] = range(99, 89, -1) :-) Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: how to append to a list twice?
Fredrik Lundh <[EMAIL PROTECTED]> wrote: > Alex Martelli wrote: > > > > But of course that only does it once, and I don't want to have to copy > > > and paste the append line. Perhaps there's a better way than this. > > > > def makeseries(N): > > series = [N] > > append = series.append > > for tailer in xrange(N-1, -1, -1): > > append(tailer) > > append(tailer) > > But Now You've Violated The DRY Principle!!! Just as with any other unrolled loop, yes -- loop unrolling is an optimization which is based exactly on exchanging some textual repetition for a tiny bit more speed. Of course, optimizations can easily be premature, and in any case need to be checked by measurement. E.g., here are a few variations: def makeseries_a(N): series = [N] append = series.append for tailer in xrange(N-1, -1, -1): append(tailer) append(tailer) return series def makeseries_b(N): series = [N] append = series.append for tailer in xrange(N-1, -1, -1): for x in (1,2): append(tailer) return series def makeseries_c(N): series = [N] extend = series.extend for tailer in xrange(N-1, -1, -1): extend((tailer,tailer)) return series def makeseries_d(N): series = [N] extend = series.extend for tailer in xrange(N-1, -1, -1): extend((tailer,)*2) return series And: brain:~/downloads alex$ python -mtimeit -s'import rep' 'rep.makeseries_a(100)' 1 loops, best of 3: 31.7 usec per loop brain:~/downloads alex$ python -mtimeit -s'import rep' 'rep.makeseries_b(100)' 1 loops, best of 3: 57.4 usec per loop brain:~/downloads alex$ python -mtimeit -s'import rep' 'rep.makeseries_c(100)' 1 loops, best of 3: 36.2 usec per loop brain:~/downloads alex$ python -mtimeit -s'import rep' 'rep.makeseries_d(100)' 1 loops, best of 3: 54.4 usec per loop So, it would seem that (at least among these few variations) I had guessed right, this time -- the loop-unrolling is beneficial and append is minutely better than extend. Of course, the yanking from the loopbody of the boundmethod is also a key optimization here -- with unyanked (more natural) versions [[i.e., calling series.append or series.extend right in the loop body]] I measure: brain:~/downloads alex$ python -mtimeit -s'import rep' 'rep.makeseries_a(100)' 1 loops, best of 3: 57.3 usec per loop brain:~/downloads alex$ python -mtimeit -s'import rep' 'rep.makeseries_b(100)' 1 loops, best of 3: 83.5 usec per loop brain:~/downloads alex$ python -mtimeit -s'import rep' 'rep.makeseries_c(100)' 1 loops, best of 3: 48 usec per loop brain:~/downloads alex$ python -mtimeit -s'import rep' 'rep.makeseries_d(100)' 1 loops, best of 3: 68.4 usec per loop Alex -- http://mail.python.org/mailman/listinfo/python-list
Re: how to append to a list twice?
Alex Martelli wrote: > > But Now You've Violated The DRY Principle!!! > > Just as with any other unrolled loop, yes -- loop unrolling is an > optimization which is based exactly on exchanging some textual > repetition for a tiny bit more speed. I had hoped that the Unusual Capitalization would have been enough to make up for the missing smiley... (but note that the OP explicitly didn't want to copy and paste; given that he wrote a newsgroup posting instead seems to indicate that his programming editor isn't quite as good as the editor in his newsreader) -- http://mail.python.org/mailman/listinfo/python-list
Re: how to append to a list twice?
"Fredrik Lundh" wrote: > Alex Martelli wrote: > > > > But of course that only does it once, and I don't want to have to copy > > > and paste the append line. Perhaps there's a better way than this. > > > > def makeseries(N): > > series = [N] > > append = series.append > > for tailer in xrange(N-1, -1, -1): > > append(tailer) > > append(tailer) > > But Now You've Violated The DRY Principle!!! Do you mean the "three times -1" in the xrange arguments? :-) pepr -- http://mail.python.org/mailman/listinfo/python-list