I need to save a large list of integers in Bigtable (db). For efficiency, I store them as the difference between two consecutive elements.
eg:
original_list = [1005, 1004, 1003, 1004, 1006]
Saving the specified list (actually contains more than 1000 thousand items) as
start = 1005 diff = [-1, -1, 1, 2]
The closest I could manage
ltp = [start] map (lambda x: ltp.append (ltp [-1] + x), tick)
I am looking for an effective way to convert it to a source list.
The following works for me:
orig = [start] for x in diff: orig.append(orig[-1] + x)
map , None. for , , .
map
None
for
numpy . 200 (. ), ,
add.accumulate(diff)
numpy direct list:
import numpy as nx import timeit N = 10000 diff_nx = nx.zeros(N, dtype=nx.int) diff_py = list(diff_nx) start = 1005 def f0(): orig = [start] for x in diff_py: orig.append(orig[-1] + x) def f1(): diff_nx[0] = start nx.add.accumulate(diff_nx) t = timeit.Timer("f0()", "from __main__ import f0, f1, diff_nx, diff_py, nx, start") print t.timeit(number=1000) t = timeit.Timer("f1()", "from __main__ import f0, f1, diff_nx, diff_py, nx, start") print t.timeit(number=1000)
13.4044158459 # for list looping 0.0474112033844 # for numpy accumulate
, , , PyTables, , , .
, , , , , .
:
def diff2abs( diffs, start ): yield start for diff in diffs: start += diff yield start start = 1005 diffs = [-1, -1, 1, 2] original_list = list( diff2abs( diffs, start ))
, , , , .
(.. ), - POV- Python, "diff" [-1, -1, 1, 2] , [1005, 1004, 1003, 1004, 1006].
[-1, -1, 1, 2]
[1005, 1004, 1003, 1004, 1006]
class runningtotal: def __init__(self, start = 0): self.total = start def __call__(self, value): self.total += value return self.total
>>> map(runningtotal(start), [0,]+diff) [1005, 1004, 1003, 1004, 1006]
mshsayem, - , , , /lambdas ( Mark Lutz Learning Python).
FP-ish-, "", [ ] Python, ( ).
"" , "" .
, - :
scan(lambda x,y: x+y, [start]++diff)
, , , for :
l = [start] for i in diff: l.append(l[-1] + i)
, diffs-rcoder , , , , , . , " ", , , . , :
start = 1005 def mod_start(x): global start start += x return start int_generator = (mod_start(i) for i in diffs)
int_generator, , . , , , .
, . mod_start.
: . yield, THC4k. , , . , list().
, .
start = 1005 diffs = [-1,-1,1,2] reduce(lambda undiffed_list, diff: undiffed_list + [undiffed_list[-1] + diff],diffs,[start])
Source: https://habr.com/ru/post/1716609/More articles:Does encapsulation severely degrade performance? - performanceJava - add key / value to map within map in 1 line of code? - javaStreaming Video with Blackberry Simulator - video-streamingASP.Net MVC, ORM, and heavy objects - asp.net-mvcShould I generate a complex object in the database or at the data access level? - databaseLINQ2SQL - перекрестное соединение, когда я хочу внутреннее соединение - linqUnable to connect to SQL Server 2008 Express from Virtual PC - visual-studio-2008How to pass a parameter to silverlight so that it is behind the scenes - asp.netTouch events not working in UIViews inside UIScrollView - iphoneExclusively open / modify XML file? - c #All Articles