Python complex formula using the items of a dictionary (or a list of dictionaries)
1
2
Entering edit mode
2.2 years ago
Bianca ▴ 20

I have a list with 20 dictionaries that are similar to these:

dic1 = {"aa":1, "ba":1, "ca":1, "dd":2, "ee":2, "fa":2, "ga":4, "hh":4, "ia":1}

dic2 = {"aa":1, "bc":1, "cd":2, "dd":2, "ea":2, "fg":2, "gk":4, "hh":4, "mb":5}

dic3 = {"ab":1, "bd":2, "cm":2, "dj":2, "ej":2, "fa":2, "gg":2, "ha":4}


I have to apply the same formula to each dictionary and I would like to output a list with the results of the formula per dictionary (thus, a list with 20 integers).

The formula will vary according to the "value" amount:

In summary, per dictionary I need to calculate final_formula:

If value == 1, then my partial formula is: formula1 = (number of unique keys that contain value 1) [1 - x + x ((x-10/x) ^1]

If the value is anything above 1 (else), then my intermediary formulas are formulan = (number of unique keys with value i) [(value i) - x + x ((x-10/x)^(value i)]

final_formula = formula 1 + formula2 + formula3 (n formulas, where n is the number of distinct values =! 1). X is a constant that I will define. The output should be an integer.


A practical example: For my dic1, I have 4 keys with value ==1, 3 keys with value ==2, 2 keys with value ==4 The formula for dic1 thus is:

final_formula_dic1 = 4 [1 - x + x ((x-10/x)^1] + 3 [2 - x + x ((x-10/x)^2] + 2 * [4 - x + x ((x-10/x)^4]

For my dic2, I have 2 keys with value ==1, 4 keys with value == 2, 2 keys with value == 4, 1 key with value == 5 The formula for dic2 thus is:

final_formula_dic2 = 2 [1 - x + x ((x-10/x)^1] + 4 [2 - x + x ((x-10/x)^2] + 2 * [4 - x + x ((x-10/x)^4]

For my dic3, I have 1 key1 with value ==1, 6 keys with value == 2, 1 key with value == 4 The formula for dic3 thus is:

final_formula_dic3 = 1 [1 - x + x ((x-10/x)^1] + 6 [2 - x + x ((x-10/x)^2] + 1 * [4 - x + x ((x-10/x)^4]

I am applying each formula manually to each dictionary, but I have too many dictionaries in my list and this is error prone.

I would really like to iterate over all dictionaries and apply the formula (since it is the same criteria to all dictionaries) and create a list with results, for example, list = ['final_formula_dic1', 'final_formula_dic2', 'final_formula_dic3'...] which would be integers, for instance: list = ['3000', '3200', '1300'...].

I hope this makes sense. Thank you very much in advance!

python dictionary • 1.5k views
ADD COMMENT
1
Entering edit mode

This has no connection to bioinformatics, and appears to be an ordinary programming problem. Given your level of explanation and understanding, I think you should be able to figure it out with some research.

ADD REPLY
5
Entering edit mode
2.2 years ago

Hope bioinformatics will benefit.

X = 2     # Define X here
DICTS = [
    {"aa":1, "ba":1, "ca":1, "dd":2, "ee":2, "fa":2, "ga":4, "hh":4, "ia":1},
    {"aa":1, "bc":1, "cd":2, "dd":2, "ea":2, "fg":2, "gk":4, "hh":4, "mb":5},
    {"ab":1, "bd":2, "cm":2, "dj":2, "ej":2, "fa":2, "gg":2, "ha":4},
]

for d in DICTS:
    dt = {}
    for key, value in d.items():
        if value not in dt:
            dt[value] = set()
        dt[value].add(key)

    final_result = 0
    final_formula = []
    for value in sorted(dt):
        n = len(dt[value])
        formula = f'{n} * ({value} - X + X * (X - 10 / X^{value})'
        final_formula.append(formula)
        result = n * (value - X + X * (X - 10 / X) ** value)
        final_result += result

    print(" + ".join(final_formula))
    print(final_result)

Output:

4 * (1 - X + X * (X - 10 / X^1) + 3 * (2 - X + X * (X - 10 / X^2) + 2 * (4 - X + X * (X - 10 / X^4)
354.0
2 * (1 - X + X * (X - 10 / X^1) + 4 * (2 - X + X * (X - 10 / X^2) + 2 * (4 - X + X * (X - 10 / X^4) + 1 * (5 - X + X * (X - 10 / X^5)
-97.0
1 * (1 - X + X * (X - 10 / X^1) + 6 * (2 - X + X * (X - 10 / X^2) + 1 * (4 - X + X * (X - 10 / X^4)
256.0
ADD COMMENT
1
Entering edit mode

I am always reminded that there are star programmers, and more importantly kind people, who will answer even questions that are not meant for this forum.

ADD REPLY
0
Entering edit mode

Thank you for the kind words. I am delighted to hear them from you, Mensur Dlakic .

ADD REPLY
0
Entering edit mode

Thank you very much for your help

ADD REPLY

Login before adding your answer.

Traffic: 1604 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6