python - Divide a dict by a number (int) -
i have problem, have create fuction divide dict int. dict:
counter({1: [9, 10, 1], 2: [5, 1, 1, 2, 1, 1, 9, 1, 1, 1, 3, 1, 1, 1, 2, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 10, 1, 1, 1, 1, 1, 3, 1, 1, 2, 1, 1, 1, 3, 4, 1, 1, 1, 3, 1, 4, 1, 1, 1, 1], 0: [1, 5, 1, 1, 2, 10, 1, 2, 1, 2, 2, 1, 1]})
and function:
def probabilitacondizionata(lista, sommafreq): lista= {k: v / sommafreq k, v in lista.items()} return lista
and function, sum value(int) sommafreq:
def sommafrequenze(lista): sommafreq= sum(lista.values()) return sommafreq
this instruction gives me error: unsupported operand type(s) /: 'dict_values' , 'int' ..
the output like:
counter({1: [9/sommafreq, 10/sommafreq, 1/sommafreq], 2: [5/sommafreq, 1sommafreq, 1/sommafreq, 2/sommafreq, 1/sommafreq, 1/sommafreq, 9, 1, 1, 1, 3, 1, 1, 1, 2, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 10, 1, 1, 1, 1, 1, 3, 1, 1, 2, 1, 1, 1, 3, 4, 1, 1, 1, 3, 1, 4, 1, 1, 1, 1], 0: [1, 5, 1, 1, 2, 10, 1, 2, 1, 2, 2, 1, 1]})
and keep on going divide all.. sorry bad english , in advance!
edit: previous functions:
def ricercafrequenze(trainspam, testspam): filtratespam=counter() filtratespam ={k:v (k,v) in trainspam.items() if k in testspam} return filtratespam #main def ricerchefrequenzeinlista(lista1,lista2): lista=counter() i=0 while < len(lista2): lista[i]=(ricercafrequenze(lista1, lista2[i])) i+=1 return lista counter({0: {'offer': 1, 'time': 5, 'discount': 1, 'one': 1, 'th': 2, 'subject': 10, 'special': 1, 'need': 2, 'price': 1, 'order': 2, 'product': 2, 'per': 1, 'today': 1}, 1: {'us': 9, 'subject': 10, 'buy': 1}, 2: {'time': 5, 'realist': 1, 'quickli': 1, 'give': 2, 'go': 1, 'thoma': 1, 'us': 9, 'let': 1, 'aruba': 1, 'natur': 1, 'b': 3, 'length': 1, 'ca': 1, 'one': 1, 'know': 2, 'life': 1, 'think': 1, 'girlfriend': 1, 'use': 2, 'stronger': 1, 'press': 1, 'longer': 1, 'fl': 1, 'po': 1, 'partner': 1, 'subject': 10, 'presid': 1, 'save': 1, 'nearli': 1, 'stud': 1, 'short': 1, 'everi': 3, 'gain': 1, 'citi': 1, 'product': 2, 'said': 1, 'increas': 1, 'month': 1, 'unit': 3, 'want': 4, 'must': 1, 'sex': 1, 'pleasur': 1, 'result': 3, 'matt': 1, 'name': 4, 'love': 1, 'bigger': 1, 'visitor': 1, 'oranjestad': 1}})
as mentioned in comment, can evaluate sum , divide value achieve same thing. anyways, can divide every element this:
counter({k:[i/sommafreq in v] k,v in lista.items()})
Comments
Post a Comment