【问题标题】:Divide a dict by a number (int)将一个字典除以一个数字(int)
【发布时间】:2016-11-13 08:55:07
【问题描述】:

我有一个问题,我必须创建一个函数,将我的 dict 除以一个 int。 这是我的字典:

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]})

这是我的功能:

def probabilitacondizionata(lista, sommafreq):
    lista= {k: v / sommafreq for k, v in lista.items()}
    return lista

这是函数,它对 value(int) sommafreq 求和:

def sommafrequenze(lista):
    sommafreq= sum(lista.values())
    return sommafreq

这条指令给了我这个错误:不支持的操作数类型 for /: 'dict_values' and 'int' ..

我想要的输出是这样的:

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]})

并继续划分所有..对不起我的英语不好,提前谢谢!

编辑:我以前的功能:

def ricercafrequenze(trainspam, testspam):
    filtratespam=Counter()
    filtratespam ={k:v for (k,v) in trainspam.items() if k in testspam}

    return filtratespam
    #main
def ricerchefrequenzeinlista(lista1,lista2):
    lista=Counter()
    i=0
    while i < 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}})

【问题讨论】:

  • 你为什么不直接除法呢?您需要划分列表中的每个项目:{k: [x / sommafreq for x in v] for ... },但您当前的代码不应给出您声称的错误。你能提供一个带有完整回溯的minimal reproducible example吗?
  • 谢谢,但我给了我这个错误:AttributeError: 'dict_values' object has no attribute 'items'
  • 这不是我的建议的问题,;lista 显然是字典的值,而不是字典本身。

标签: python dictionary int counter divide


【解决方案1】:

如果sommafrequenze 函数专用于总结初始列表的值 - 使用以下简化方法:

def probabilitacondizionata(lista, sommafreq):
    lista = {k: [v / sommafreq for v in v] for k, v in lista.items()}
    return lista

l = {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]}
l_sum = sum(v for sublist in l.values() for v in sublist)

print(probabilitacondizionata(l, l_sum))

输出:

{0: [0.007194244604316547, 0.03597122302158273, 0.007194244604316547, 0.007194244604316547, 0.014388489208633094, 0.07194244604316546, 0.007194244604316547, 0.014388489208633094, 0.007194244604316547, 0.014388489208633094, 0.014388489208633094, 0.007194244604316547, 0.007194244604316547], 1: [0.06474820143884892, 0.07194244604316546, 0.007194244604316547], 2: [0.03597122302158273, 0.007194244604316547, 0.007194244604316547, 0.014388489208633094, 0.007194244604316547, 0.007194244604316547, 0.06474820143884892, 0.007194244604316547, 0.007194244604316547, 0.007194244604316547, 0.02158273381294964, 0.007194244604316547, 0.007194244604316547, 0.007194244604316547, 0.014388489208633094, 0.007194244604316547, 0.007194244604316547, 0.007194244604316547, 0.014388489208633094, 0.007194244604316547, 0.007194244604316547, 0.007194244604316547, 0.007194244604316547, 0.007194244604316547, 0.007194244604316547, 0.07194244604316546, 0.007194244604316547, 0.007194244604316547, 0.007194244604316547, 0.007194244604316547, 0.007194244604316547, 0.02158273381294964, 0.007194244604316547, 0.007194244604316547, 0.014388489208633094, 0.007194244604316547, 0.007194244604316547, 0.007194244604316547, 0.02158273381294964, 0.02877697841726619, 0.007194244604316547, 0.007194244604316547, 0.007194244604316547, 0.02158273381294964, 0.007194244604316547, 0.02877697841726619, 0.007194244604316547, 0.007194244604316547, 0.007194244604316547, 0.007194244604316547]}

【讨论】:

  • 谢谢,但总是同样的问题,文件“C:/Users/david/prova.py”,第 141 行,在 probabilitacondizionata {k: [v / sommafreq for v in v] for k, v在 lista.items()} AttributeError: 'dict_values' 对象没有属性 'items'
  • 您已将您的字典显示为Counter 参数,您能以实际形式显示初始字典吗?将您的 dict 与我的答案中的那个进行比较
【解决方案2】:

正如评论中提到的,您可以只计算总和,然后除以一个值来实现相同的目标。无论如何,您可以像这样划分每个元素:

Counter({k:[i/sommafreq for i in v] for k,v in lista.items()}) 

【讨论】:

  • 谢谢,但我给了我这个错误:AttributeError: 'dict_values' object has no attribute 'items' –
  • @ErikGodard,没有 python 2 也有 items(),所以这不是 python 2/3 问题。可能是 OP 在列表中应用 .items()
  • 不,我使用 python 3.5 版 :(
  • 可能是sommafreq类型的问题?这是一个整数
  • @DavideDiMenna,不,我已经用 int 值对其进行了测试,这很好。问题就在你身边。你能告诉我们print type(lista)的输出吗?
【解决方案3】:

你有两种可能:

1。使用numpy(因为你有字典作为列表值,这不适用):

    import numpy as np
    def probabilitacondizionata(lista, sommafreq):
        lista= {k: np.array(list(v)) / sommafreq for k, v in lista.items()}
        return lista

2. 使用dict理解:

    def probabilitacondizionata(lista, sommafreq):
        lista= {k: {k_i: v_i / sommafreq for k_i, v_i in v.items()} for k, v in lista.items()}
        return lista

【讨论】:

  • 感谢这两个解决方案,我尝试了第一个:TypeError: unsupported operand type(s) for /: 'dict_values' and 'int' 和第二个但是,它给了我这个错误:AttributeError: 'dict_values' 对象没有属性 'items' –
  • 这是由于使用了 Python 3。尝试将 v 包装在一个列表中,就像在编辑中一样
  • 它给了我同样的错误,AttributeError: 'dict_values' object has no attribute 'items'.. 但是计数器不是 dict.. 可能是这个问题吗?
  • 错误表明lista 的类型为dict_values(因此不是字典)。你得到的错误是因为你调用probabilitacondizionata 没有字典,而是它的值作为参数
  • 我发现这是一个 my lista
【解决方案4】:

我经常想在不使用字典推导的情况下做这种事情。 BetterDict 是我的一个小项目,它扩展了 Python dicts/Counters/defaultdicts/OrderedDicts,允许它们被合并,或者在算术中与标量或其他 dicts 一起使用。 例如

>>> raw_counts = BetterDict({'the': 1432, 'she': 600, 'wookie': 25})
>>> total = 10000.
>>> relative_freqs = raw_counts / total
>>> print(relative_freqs)
{'she': 0.06, 'the': 0.1432, 'wookie': 0.0025}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-30
    • 1970-01-01
    • 1970-01-01
    • 2019-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多