Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中查找字典中要求的最小和之上的最小和_Python_Dictionary - Fatal编程技术网

在python中查找字典中要求的最小和之上的最小和

在python中查找字典中要求的最小和之上的最小和,python,dictionary,Python,Dictionary,首先,我觉得有必要解释这个问题的动机。最近,我在手机上玩了一个游戏,主要目的是在选举中赢得270张选票,成为总统候选人。为了赢得某一类别的支持,你必须赢得这些州以美元计算的大部分权重 states_dict = { 'CA': 200, 'TX':200, 'NY': 150, 'FL': 100, 'IL': 50, 'AR':24, 'CO': 18, 'NM': 15, 'NV': 17.5 , 'NJ': 42.5} 因此,我关心的是,在这些州所属的类别中,所需的最低金额。这使得词典成

首先,我觉得有必要解释这个问题的动机。最近,我在手机上玩了一个游戏,主要目的是在选举中赢得270张选票,成为总统候选人。为了赢得某一类别的支持,你必须赢得这些州以美元计算的大部分权重

states_dict = { 'CA': 200, 'TX':200, 'NY': 150, 'FL': 100, 'IL': 50, 'AR':24, 'CO': 18, 'NM': 15, 'NV': 17.5 , 'NJ': 42.5}
因此,我关心的是,在这些州所属的类别中,所需的最低金额。这使得词典成为解决这个问题的最佳选择。下面是一本以州首字母为键的字典,其重量以美元为单位

states_dict = { 'CA': 200, 'TX':200, 'NY': 150, 'FL': 100, 'IL': 50, 'AR':24, 'CO': 18, 'NM': 15, 'NV': 17.5 , 'NJ': 42.5}
在开始时,我们必须找到字典条目的总和并将其除以2。如果部分和小于半和,我们必须继续相加,一旦超过或达到半和,我们必须停止并将其与之前的最小值进行比较。如果它更小,我们必须用新发现的最小值替换当前的最小值。

我是字典的新手,我只知道如何遍历字典,但在这里我必须计算列表中的每个部分和。有没有办法计算出这个列表中可能的最小和?

好的,首先我想我可以简单地按升序排序,然后将它们相加,直到得到一个大于所需最小值的值,然而,我意识到,你甚至可以使用最大的值,将其添加到最小的值,得到一个比你添加排序值时得到的值低的值,对吗

这对我来说意味着,如果你有10个值,你需要得到不同的组合,这就是选择这些值的所有方法,这样你可以得到9个不同数字的不同总和,8个数字,7个数字……最多2个数字,或者1个数字

因此,这段代码的工作原理是获得9个数字到1个数字的不同组合…10个数字没有用处,因为如果你只有10个数字,无论你如何选择它们,你仍然会有相同的数字总和

在代码中,我添加了注释,以准确显示这些步骤的位置以及我是如何执行这些步骤的

    import itertools
    # itertools is used to generate all possible combinations for the values, its there automatically 
    # you don't need to download it
    states_dict = { 'CA': 200, 'TX':200, 'NY': 150, 'FL': 100, 'IL': 50, 'AR':24, 'CO': 18, 'NM': 15, 'NV': 17.5 , 'NJ': 42.5}
    
    #calculating the minimum by dividing total by 2
    #getting the sum of all the values in the dictionary
    total = sum(states_dict.values())
   
    #dividing the sum by 2 to get the minimum
    minimum = total/2

    #displaying the minimum
    print("The minimum value is: {}".format(minimum))
    #Output: The minimum value is: 408.5
    
    # storing the values only in a list, so that its easier to work with them
    values = list(states_dict.values())

    #empty list that will store all the combinations from 9 numbers to 1 number
    all_combinations = []
    
    #empty list that will store all the sums of the different combination, the sum of each combination is stored as a different element
    all_sums = []
    
    #generates a list of all possible combinations
    #r is number of numbers that is involved in the combination
    #when r is 5 it will get all the ways that five different numbers from the list of 10 numbers can be selected
    # the length here is 10, so the combinations will only reach 9 numbers
    for r in range(len(values)):
        #here is where i used itertools to generate the combinations for me (nCr)
        combinations_object = itertools.combinations(values, r) #takes iterable(values) and r(number of numbers)

       #convert the itertools object to a list, so that we can add it to the bigger list, also so that we can get the sum of the elements easily kater
        combinations_list = list(combinations_object)

        # adding the new combination to the bigger list, which makes a 2d array
        all_combinations += combinations_list
        
    #generates a list of the sums of the values in the combinations        
    for objects in all_combinations:
        #the sum function here adds the values in each combination
        sum_object = sum(list(objects))
        #adding the sum into an array which holds the sums of the different combinations
        all_sums.append(sum_object)
     
    
    #sorts the list of sums in ascending order (smallest sum to largest sum)
    all_sums = sorted(all_sums)
    
    #gets lowest sum that is above the minimum
    
    #now that the list of sums is sorted, we just iterate through it so that immediately it gets a sum that is above the minimum value(408.5) it captures it and stores it in a variable (required value)
    for sum_object in all_sums:
        if (sum_object>minimum):
            # store required value once the loop reaches it
            required_value = sum_object
            #display the required value (409.5)
            print("The required value is: {}".format(sum_object),end="\n")
            #break out of the loop because we've got what we wanted and don't need to continue looping
            break
      
    #prints out the combinations whose sum is the required value
    #re-using the list of combinations now that we have the value, we just check whether the sum of the values inside the combination is the same as the required value, and if it is we print it out
    print("\nThe combinations are: ")
    for objects in all_combinations:
        #if the sum of number in combination is same as required value
        if (sum(list(objects)) == required_value):
            #display the items in that combination
            print(objects,end="\n")
            
    #Output
    # The minimum value is: 408.5
    # The required value is: 409.5
    
    # The combinations are: 
    # (17.5, 18, 24, 150, 200)
    # (17.5, 18, 24, 150, 200)
    # (17.5, 18, 24, 50, 100, 200)
    # (17.5, 18, 24, 50, 100, 200)
我没有继续显示将有这个值的状态,因为考虑到询问的人只要求这个值,我认为这会使代码读起来非常冗长乏味


因此,我希望这就是您所寻找的,它能帮助您

什么是“部分和”和“半和”?@Devanssoni“部分和”指字典中可能的每一个和,例如TX+CA、CO+NM、CO+NM+TX+AR等值。简言之,每个可能的和,包括存在于dict@DevanshSoni半和就是和除以2。如果你收集的状态总和大于一半,你就赢得了该组的支持,你能用一个example@deadshot当然假设我们有下面的dict{'CA':200,'TX':200,'NY':150,'FL':100,'IL':50}。总数是700,因此一半的总数是350。程序(或函数)必须打印(或返回)一个状态为其附加权重等于350的字符串。在这种情况下,“CA+NY”有不止一种组合,但在这种情况下,这并不重要。一个仅代码的答案可能有助于立即执行OP,但StackOverflow并不意味着仅执行OP。因此,网站不适合人们众包其代码,并且仅代码的答案鼓励此类行为。一个好的答案包括解释此代码如何/为什么解决问题。哦,请让我们继续在Youtube上进行投票。哇……对不起,布鲁克,让我编辑代码来解释它是如何工作的……我只是刚开始堆叠,这就是为什么我没有做太多解释,我不知道如何很好地使用这些功能