简体   繁体   English

如何使用Python defaultdict将一个值除以另一个?

[英]How can I use a Python defaultdict to divide one value by another?

I have a defaultdict built with keys and values, but I need to be able to divide the first value by the second value if there are two values in the pair. 我有一个使用键和值构建的defaultdict,但是如果该对中有两个值,则需要能够将第一个值除以第二个值。

defaultdict(<type 'list'>, {3: [7567, 6525], 4: [0], 65542: [609, 5245], 13:     
[73585, 84764], 14: [159, 19385], 65552: [1834], 22: [47333], 25: [0, 5320],
65562: [0], 98332: [0], 30: [0, 704249], 32: [5612], 33: [76050]}

So, for 3:, I would need to get 7567/6525, and put that into a new dictionary with the same key. 因此,对于3:我将需要获得7567/6525,并将其放入具有相同键的新字典中。 But, for 32, I can't do any division, so I'd need to remove it from the set. 但是,对于32,我无法进行任何除法,因此需要将其从集合中删除。

How would I go about doing this, knowing I don't always have 2 values to divide with? 知道我并不总是有2个值可除时,我将如何去做?

Use a dictionary comprehension and the ternary operator. 使用字典理解和三元运算符。

Python 3 Python 3

from operator import truediv

{key: (truediv(*val) if len(val) == 2 else val[0]) for key, val in dic.items()}

Python 2 Python 2

from operator import truediv

{key: (truediv(*val) if len(val) == 2 else val[0]) for key, val in dic.iteritems()}

I'll continue with Python 3. If you want to remove those entries with less than 2 items 我将继续使用Python3。如果要删除条目少于2个的条目

{key: div(*val) for key, val in dic.items() if len(val) == 2}

Update per your comment under the question 根据您对问题的评论进行更新

Wrapping the results back into lists and getting a list of the "outliers". 将结果包装回列表中,并获得“异常值”列表。

new_dic = {key: [div(*val)] for key, val in dic.items() if len(val) == 2}
outliers = [key for key, val in dic.items() if len(val) != 2]

I guess since everyone else is interpreting the question this way I'll post my version as an answer: 我猜因为其他所有人都以这种方式解释问题,因此我将发布我的版本作为答案:

{k: reduce(operator.div, map(float, v)) for k, v in original.items()}

Alternatively: 或者:

{k: reduce(operator.truediv, v) for k, v in original.items()}

Where original is your dict and you import operator . original dict是哪里,您要import operator If you're on Python 3 you'll need to from functools import reduce as well (global in Python 2). 如果您使用的是Python 3,则还需要from functools import reduce (在Python 2中是global)。

Also, if you're using Python 3, you can drop map(float, v) in favor of just v . 另外,如果您使用的是Python 3,则可以放下map(float, v) ,而只支持v That's so you don't get burned by integer division (Floor division) on Python 2. You can use truediv instead of map to avoid this -- I was thinking, incorrectly, this was not portable between versions of Python. 这样一来,您就不会在Python 2上被整数除法(Floor除法)所 truediv 您可以使用truediv而不是map来避免这种情况-我在想,这是不正确的,这在Python版本之间是不可移植的。 I stand corrected. 我站得住了。

Note: This will not work if your dict values are not always iterable. 注意:如果您的dict值并非总是可迭代的,则此方法将无效。 You're using a defaultdict with a list default so you'll be fine. 您正在使用带有列表默认值的defaultdict,这样就可以了。

You could do something like this: 您可以执行以下操作:

for k in mydict.keys():
  if len(mydict[k]) != 2:
    mydict.pop(k)
  else:
    v1, v2 = mydict[k]
    mydict[k] = v1 / v2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何使用一个变量的值在python中调用另一个变量? - How can i use the value of one variable to call another in python? 如何在defaultdict中使用两个键? - How can I use two keys with defaultdict? 如何在字典中的值上过滤此python defaultdict? - How do I filter this python defaultdict on a value within a dict? 我如何测试以查看Python中的defaultdict(set)是否为空 - How can I test to see if a defaultdict(set) is empty in Python Python:如何在一个模块中初始化对象并在另一个模块中使用它 - Python: How can I initialise object in one module and use it in another 如何将defaultdict(Set)转换为defaultdict(list)? - How can I convert defaultdict(Set) to defaultdict(list)? 我可以在Python中将defaultdict或dict转换为ordereddict吗? - Can I convert a defaultdict or dict to an ordereddict in Python? 如何将一列除以另一列,其中一个数据帧的列值对应于 Python Pandas 中另一个数据帧的列值? - How to divide one column by another where one dataframe's column value corresponds to another dataframe's column's value in Python Pandas? 如何在python的另一个应用程序中使用函数返回的值? - How can I use a value returned by a function in another application in python? Python - 如何将字典作为值传递给defaultdict而不是作为引用 - Python - how to pass a dictionary into defaultdict as value and not as a reference
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM