Function returns

TypeError: 'float' object has no attribute ' getitem '

on the line profile[symbol][j] = Probabilities[symbol][j] / t . Why?

 Probabilities = {'A': 0.1, 'C': 0.1, 'G': 0.1, 'T': 0.1} def Normalize(Probabilities): t = len(Probabilities) k = 1 profile = {} for symbol in "ACGT": profile[symbol] = [] for j in range(k): profile[symbol].append(0) for symbol in "ACGT": for j in range(k): profile[symbol][j] = Probabilities[symbol][j] / t return profile print(Normalize(Probabilities)) 

    2 answers 2

    __getitem__ method implements the seq[i] operation (address by index). float type (floating point numbers) does not support indexing in Python.

    You have a dictionary of Probabilities , which returns a float on a given symbol. For example, Probabilities['A'] returns 0.1 . It is not clear what you would expect when trying to index a float: (0.1)[j] .

       type(Probabilities[symbol]) >>> <class 'float'> # Probabilities[symbol][j]