简体   繁体   English

我需要一些帮助,将字典用作 function 参数

[英]I need some help using a dictionary as a function parameter

I need to replicate this same function but instead of having a list as a parameter I need a dictionary.我需要复制同样的 function 但不是将列表作为参数,我需要一个字典。 The idea is that the calculation done by the function is done with the values, and the function returns the keys.这个想法是 function 完成的计算是使用值完成的,function 返回键。

def funcion(dic, Sum):
    
    Subset = []
    
    def f(dic, i, Sum):
        if i >= len(dic): return 1 if Sum == 0 else 0
        count = f(dic, i + 1, Sum)
        count += f(dic, i + 1, Sum - dic[i])
        return count

    for i, x in enumerate(dic):
        if f(dic, i + 1, Sum - x) > 0:
            Subset.append(x)
            Sum -= x
    return Subset

The function works if I enter (300, 200,100,400).如果我输入 (300, 200,100,400),function 就可以工作。 But i need to use as an input something like {1:300, 2:200, 3:100, 4:400 } So the calculation is done with the values, but it returns the keys that match the condition.但我需要使用像 {1:300, 2:200, 3:100, 4:400 } 这样的输入,所以计算是用值完成的,但它返回匹配条件的键。

Im trying working with dic.keys() and dic.values() but its not working.我正在尝试使用 dic.keys() 和 dic.values() 但它不起作用。 Could you help me?你可以帮帮我吗?

Thank u so much.非常感谢你。

Your code isn't working with your dictionary because it's expecting to be able to index into dic with numeric indexes starting at 0 and going up to len(dic)-1 .您的代码不适用于您的字典,因为它希望能够使用从0开始并上升到len(dic)-1的数字索引来索引dic However, you've given your dictionary keys that start at 1 and go to len(dic) .但是,您已将字典键以1和 go 开头给len(dic) That means you need to change things up.这意味着你需要改变现状。

The first change is in the recursive f function, where you need the base case to trigger on i > len(dic) rather than using the >= comparison.第一个变化是递归f function,您需要在i > len(dic)上触发基本情况,而不是使用>=比较。

The next change in in the loop that calls f .调用f的循环中的下一个更改。 Rather than using enumerate , which will generate indexes starting at 0 (and pair them with the keys of the dictionary, which is what you get when you directly iterate on it), you probably want to do something else.而不是使用enumerate ,它将生成从0开始的索引(并将它们与字典的键配对,这是您直接迭代它时得到的),您可能想做其他事情。

Now, ideally, you'd want to iterate on dic.items() , which would give you index, value pairs just like your code expects.现在,理想情况下,您希望在dic.items()上进行迭代,这将为您提供索引、值对,就像您的代码所期望的那样。 But depending on how the dictionary gets built, that might iterate over the values in a different order than you expect.但根据字典的构建方式,可能会以与您预期不同的顺序迭代值。 In recent versions of Python, dictionaries maintain the order their keys were added in, so if you're creating the dictionary with {1:300, 2:200, 3:100, 4:400 } , you'll get the right order, but a mostly-equivalent dictionary like {3:100, 4:400, 1:300, 2:200 } would give its results in a different order.在最新版本的 Python 中,字典保持添加键的顺序,因此如果您使用{1:300, 2:200, 3:100, 4:400 }创建字典,您将获得正确的顺序,但是像{3:100, 4:400, 1:300, 2:200 }这样的大多数等效字典会以不同的顺序给出结果。

So if you need to be resilient against dictionaries that don't have their keys in the right order, you probably want to directly generate the 1 - len(dict) keys yourself with range , and then index to get the x value inside the loop:因此,如果您需要对键顺序不正确的字典具有弹性,您可能希望自己使用range直接生成1 - len(dict)键,然后索引以获取循环内的x值:

for i in range(1, len(dic)+1):      # Generate the keys directly from a range
    x = dic[i]                      # and do the indexing manually.
    if f(dic, i + 1, Sum - x) > 0:  # The rest of the loop is the same as before.
        Subset.append(x)
        Sum -= x

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM