0

I'm trying to divide a dictionary by another dictionary and I cannot find a solution online that works.

Code:

def getRatio(price_a, price_b):
    """ Get ratio of price_a and price_b """
    """ ------------- Update this function ------------- """
    """ Also create some unit tests for this function in client_test.py """
    if (price_b == 0):
        # avoid zero division error
        return

    return price_a / price_b

Output:

line 51, in getRatio
    return price_a / price_b
TypeError: unsupported operand type(s) for /: 'dict' and 'dict'
quamrana
  • 37,849
  • 12
  • 53
  • 71

3 Answers3

4

The error you get is self-explanatory: you cannot divide two dictionaries. You can divide certain values of the dictionaries, or certain keys, but not two dict instances. The division operation is simply not defined for this data structure, it does not make sense. Check the python dictionary definition here

Kikolo
  • 212
  • 1
  • 10
0

‍‍‍‍This will help:

def get_ratio(dict1, dict2):
    try:
        return dict1['price'] / dict2['price']
    except ZeroDivisionError:
        raise ZeroDivisionError('You cannot divide anything by zero')

print(get_ratio({'price': 1000}, {'price': 10}))

output:

100.0
Hossein Heydari
  • 1,439
  • 9
  • 28
0

The following code returns the ratios as a dictionary with A's keys. If this is not the desired behavior, please clarify.

The solution using dictionary comprehension:

a = {'A':1, 'B':5, 'D':6}
b = {'X':4, 'Y':8, 'Z':4}

{key:val_a/val_b for ((key, val_a), val_b) in zip(a.items(), b.values())}

# Output:
# {'A': 0.25, 'B': 0.625, 'D': 1.5}

If you want the ratios as an array without the keys, you can use list comprehension:

[val_a/val_b for (val_a, val_b) in zip(a.values(), b.values())]
# Output for example above
# [0.25, 0.625, 1.5]
marvinschmitt
  • 346
  • 1
  • 7
  • 1st solution: return {key:val_a/val_b for ((key, val_a), val_b) in zip(price_a.items(), price_b.values())}. It produced this error: TypeError: unsupported operand type(s) for /: 'dict' and 'dict' – MalkonzoArruters Jan 12 '21 at 12:40
  • 2nd solution: return [val_a/val_b for (val_a, val_b) in zip(price_a.values(), price_b.values())]. Produced identical error. – MalkonzoArruters Jan 12 '21 at 12:41
  • But does the sample code I provided work? If it does, your dictionaries are structured in an unusual way. Please clarify the structure of the dictionaries. I assumed: `price_A={"item_1":price_1, "item_2":price_2, ...}` – marvinschmitt Jan 12 '21 at 12:51
  • print(price_a) produces the following: {'ABC': {...}, 'DEF': {...}} – MalkonzoArruters Jan 12 '21 at 20:16
  • print(price_a.values()) produces: dict_values([{'ABC': {...}, 'DEF': {...}}, {'ABC': {...}, 'DEF': {...}}]) – MalkonzoArruters Jan 12 '21 at 20:24
  • You should really reconsider your data structures. It seems like the dictionary price_a contains string keys and values that are dictionaries again. And their values are dictionaries again!? – marvinschmitt Jan 12 '21 at 20:59
  • I screwed up in what I was passing over. I figured it out. – MalkonzoArruters Jan 12 '21 at 21:06
  • Happy to hear that :) If the answer resolves your question now, please consider marking it as accepted with the gray check to the left. – marvinschmitt Jan 12 '21 at 22:13