Efficient way to use python lambda, map

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.

+3
source share
9 answers

The following works for me:

orig = [start]
for x in diff:
    orig.append(orig[-1] + x)

map , None. for , , .

+6

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, , , .

, , , , , .

+7

:

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 ))
+4

, , , , .

(.. ), - POV- Python, "diff" [-1, -1, 1, 2] , [1005, 1004, 1003, 1004, 1006].

+2
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]
+2

mshsayem, - , , , /lambdas ( Mark Lutz Learning Python).

FP-ish-, "", [ ] Python, ( ).

"" , "" .

, - :

scan(lambda x,y: x+y, [start]++diff)
+1

, , , for :

l = [start]
for i in diff:
    l.append(l[-1] + i)
0

, 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().

0

, .

start = 1005
diffs = [-1,-1,1,2]
reduce(lambda undiffed_list, diff: undiffed_list + [undiffed_list[-1] + diff],diffs,[start])

, .

0

Source: https://habr.com/ru/post/1716609/


All Articles