John John - 1 year ago 138
Python Question

How to divide a number with dictionary values

So I'm stumped. I want to divide the length of a list by the number values in a dictionary.

firstNum = 96
secondNum = 97

list = [1,2,3,4]
dictionary = {'a': 1, 'b': 2}

for x in range(0,13):
firstNum += 1
secondNum += 1

for i in range(firstNum, secondNum):
percent = len(list) / dictionary.get(chr(i))

But I get the error:

TypeError: unsupported operand type(s) for /: 'int' and 'NoneType'

As I understand it, the .get function returns the value, which should be an int? Thanks for any help!

Answer Source

Going solely by the title - Diving the list by the values in your dictionary

all you need is :

>>> l= [1,2,3,4]
>>> d = {'a': 1, 'b': 2}
>>> for i in d.values():
...     print len(l)/i

dictionary.values() gives you all the values in your dictionary as a list

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download