Iterating through a dictionary to get next values from previous values

Question:

I have this sample dataset

{'data1':200, 'data2':500}

I want to iterate this data in a range of 25, such that each iteration would give me the previous value * (1+0.05) within the iteration.

In this case the output of range of 2 would look like this:

{'data1':[200,210], 'data2':[500, 525]}

Anyone has an idea of how to go about this?

Asked By: Babz

||

Answers:

You could use a dictionary comprehension like this:

R = 25
d = {'data1': 200, 'data2': 500}
e = {k: [v * 1.05 ** i for i in range(R)] for k, v in d.items()}

print(e)

Output:

{'data1': [200.0, 210.0, 220.5, 231.52500000000003, 243.10125000000005, 255.25631250000006, 268.01912812500007, 281.4200845312501, 295.4910887578126, 310.26564319570326, 325.7789253554884, 342.0678716232628, 359.171265204426, 377.12982846464735, 395.98631988787974, 415.78563588227377, 436.5749176763874, 458.40366356020684, 481.3238467382171, 505.3900390751281, 530.6595410288844, 557.1925180803287, 585.0521439843452, 614.3047511835624, 645.0199887427406], 'data2': [500.0, 525.0, 551.25, 578.8125000000001, 607.7531250000001, 638.1407812500001, 670.0478203125002, 703.5502113281252, 738.7277218945316, 775.6641079892581, 814.447313388721, 855.1696790581572, 897.9281630110651, 942.8245711616183, 989.9657997196994, 1039.4640897056843, 1091.4372941909685, 1146.009158900517, 1203.309616845543, 1263.4750976878202, 1326.6488525722111, 1392.9812952008217, 1462.630359960863, 1535.761877958906, 1612.5499718568515]}
Answered By: Pingu
dict = {'data1':200, 'data2':500}
e= {}
for key, value in dict.items():
    v = value
    tab = []
    for i in range(25):
        tab.append(v*(1.05)**i)
    e[(key)]=tab

print(e)

OUTPUT :

{'data1': [200.0, 210.0, 220.5, 231.52500000000003, 243.10125000000005, 255.25631250000006, 268.01912812500007, 281.4200845312501, 295.4910887578126, 310.26564319570326, 325.7789253554884, 342.0678716232628, 359.171265204426, 377.12982846464735, 395.98631988787974, 415.78563588227377, 436.5749176763874, 458.40366356020684, 481.3238467382171, 505.3900390751281, 530.6595410288844, 557.1925180803287, 585.0521439843452, 614.3047511835624, 645.0199887427406], 'data2': [500.0, 525.0, 551.25, 578.8125000000001, 607.7531250000001, 638.1407812500001, 670.0478203125002, 703.5502113281252, 738.7277218945316, 775.6641079892581, 814.447313388721, 855.1696790581572, 897.9281630110651, 942.8245711616183, 989.9657997196994, 1039.4640897056843, 1091.4372941909685, 1146.009158900517, 1203.309616845543, 1263.4750976878202, 1326.6488525722111, 1392.9812952008217, 1462.630359960863, 1535.761877958906, 1612.5499718568515]}
Answered By: Clément

your can create a recursive function and a dictionnary comprehension which is faster than a loop:

d = {'data1':200, 'data2':500}
def g(value, n_range, factor):
    if n_range <= 1:
        return [value]
    else:
        return [value] + g(value * factor, n_range-1, factor)

def fct(data, n_range):
    return {
        k: g(v, n_range, factor=1.05)
        for k, v in data.items()
    }
fct(d, 2)

instead of g() you can also use a list comprehension such as:

def fct(data, n_range):
    return {
        k: [v*(1.05)**i for i in range(n_range)]
        for k, v in data.items()
    }
fct(d, 2)
Answered By: Guillaume

Using itertools and generator:

import itertools

data = {'data1':200, 'data2':500}

def iterate(x):
    while True:
        yield x
        x = x * 1.05

def f(x, n):
    return list(itertools.islice(iterate(x), n))
        
data = {k: f(v, 25) for k, v in data.items()}
print(data)
Answered By: Martin Půda
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.