I have a list like this (I'm using integer progressive numbers for simplicity, but in fact FX rates float):
list = [4,5,6,7,8,9,10,11,12] #__len__ = 9
I need to first turn offset "offset" by adding "0" to "closed" values and then starting with "off + 1" = 3 (but this number can be 3, variable) The list status of "0" to offset this list by:
offset list = [0,0,0,4,5,6,7,8,9] #__len__ = 9
I have these two possible solutions , But I'm very innovative for programming and I would like to take advice from a person in terms of the most optimized solution from a person, as well as if possible, a different solution that I have thought about Can be better than that:
# Solution 1 offset list = [] in range for (0, off): offsetList.append (0) for k (0, lane (list) - off): offset list D (list [of])
# solution2 offset list = [] to the limit (0, off): offset list. Append (0) j = 0 while lane (offset list)
Do you think which of the two is optimized (fast, secure, stable etc.) , And why do not you? Plus (I'm sure), have I thought about those two, which is better than that?
You are the best bet to not repeat the list clearly when adding zero.
def prefix1 (l, offset): return l = [0] * offset + l def prefix2 (L offset): _xrange (offset): l.insert (0, 0) return L & Gt; & Gt; & Gt; % Timeit prefix1 ([1,2,3,4,5], 500) 10000 loops, best 3: 4.87 per loop μs> & Gt; & Gt; % Timeit prefix2 ([1,2,3,4,5], 500) 10000 loops, best 3: 190 μs per loop
Sidenote : I am using iPython % timeit
magic, which uses the python's inbuilt timeit
. You can also call this time directly when this module directly Python - Time Summary - Setup 'n = 50; L = [1,2,3,4,5] '' l = [0] * n + l '
but using iPython makes things a little easier.
In Python, normally clear looping is slower than the list's depths, although it does not apply to everything, and this does not mean that you should try to write a list to understand Everything in the form See . I do not see any reason why you will use a loop, when it is clear what you want, then add the n
element to the beginning of the list; I do exactly who says that l = [0] * offset + l
its intention is very clear.
JoelCornett
from
import itertools def prefix3 (L, offset): return list (itertools.chain ([0] * offset , L) def prefix4 (L offset): Return itertools. Chain ([0] * Offset, L) Diff prefix 5 (L offset): Return to the return chain (ITROOOOOS.Ripat (0, Offset), L) & gt; & Gt;% Timeit prefix3 ([1,2,3,4,5], 500) 10000 loops, μs for best 3: 13 loops; gt; & gt; & gt;% Timeit prefix4 ( [1,2,3,4,5], 500) 10000 loops, best 3: 2.9 μs per loop & gt; & gt;% Timeit prefix 5 ([1,2,3,4,5], 500) 10000 loops, best 3: 883 ns Per Loop
Note that prefix5
is measured in nanoseconds, while others are subtle seconds.
prefix3
and prefix4 & prefix 5
that gives ittertools.chain one iterator (no random access and you can not read it on len
can not call). Therefore it includes an overhead in a list in prefix3
. If you are interested in running only on elements, then do not use any specific access by the index, you should go with the prefix5
, otherwise the prefix2
or Prefix3
.
Comments
Post a Comment