简体   繁体   English

在 Python 中使用循环组合

[英]Combination using Loops in Python

I am trying to write a loop that can take all the different combinations of ON_PEAK, MID_PEAK and OFF_PEAK consumption combinations, which will result in the sum of 114.12 of the below-mentioned equation.我正在尝试编写一个循环,它可以采用 ON_PEAK、MID_PEAK 和 OFF_PEAK 消耗组合的所有不同组合,这将导致下面提到的等式的 114.12 的总和。

I also want those combinations to be appended to a dictionary.我还希望将这些组合附加到字典中。

In the dictionary, the key = (SUM of ON_PEAK, MID_PEAK, OFF_PEAK) and the values to be the list of combinations of On_Peak, MID_PEAK and OFF_PEAK combinations.在字典中,键 = (ON_PEAK、MID_PEAK、OFF_PEAK 的总和) 和值是 On_Peak、MID_PEAK 和 OFF_PEAK 组合的组合列表。 I am new to python and struggle with loops.我是 python 的新手,并且与循环作斗争。 I appreciate whoever can provide help.我感谢任何可以提供帮助的人。

Please see the example below:请看下面的例子:

d = {}
on_kwh = range(1, 5001, 1)
mid_kwh = range(1, 5001, 1)
off_kwh = range(1, 5001, 1)


if 0.101*(on_kwh) + 0.065(off_kwh) + 0.14(mid_kwh) = 114.12:
    d[on_kwh+mid_kwh+off_kwh].append[on_kwh,off_kwh,mid_kwh] 
    
print(d)


output (Example:1 Combination)
{1000,[150,150,700]}

*Output is based on this logic: 
0.101(150) + 0.0065(150) + 0.14(700) = 114.12

you could try a brute force approach, testing all 5000^3 (125'000'000'000) possible combinations of on_kwh, mid_kwh and off_kwh.您可以尝试一种蛮力方法,测试 on_kwh、mid_kwh 和 off_kwh 的所有 5000^3 (125'000'000'000) 种可能组合。 a better approach would be to do some pruning and to abort your loops, trying a new combination, as soon as the sum exceeds your target value.更好的方法是在总和超过您的目标值时进行一些修剪并中止循环,尝试新的组合。 eg例如

d = dict()
target = 114.12
for on_kwh in range(1, 5001, 1):
    on = on_kwh * 0.101
    if on > target: # can abort here
        break
    for mid_kwh in range(1, 5001, 1):
        mid = mid_kwh * 0.14
        if on + mid > target: # can abort here
            break
        for off_kwh in range(1, 5001, 1):
            off = off_kwh * 0.065
            if on + mid + off > target: # can abort here
                break
            if on + mid + off == target:
                d.setdefault(on_kwh + mid_kwh + off_kwh, []).append([on_kwh, mid_kwh, off_kwh])

print(d) 

this will "only" test 269'348'957 possible combinations.这将“仅”测试 269'348'957 种可能的组合。

a yet better/faster approach - in theory - would be to drop the last loop entirely: to be a solution for on_kwh * 0.101 + mid_kwh * 0.14 + off_kwh * 0.065 = 114.12 , off_kwh must be off_kwh = (114.12 - on_kwh * 0.101 - mid_kwh * 0.14) / 0.065 for any given pair of values on_kwh and mid_kwh.理论上更好/更快的方法是完全放弃最后一个循环:作为on_kwh * 0.101 + mid_kwh * 0.14 + off_kwh * 0.065 = 114.12的解决方案,off_kwh 必须是off_kwh = (114.12 - on_kwh * 0.101 - mid_kwh * 0.14) / 0.065对于任何给定的值 on_kwh 和 mid_kwh, off_kwh = (114.12 - on_kwh * 0.101 - mid_kwh * 0.14) / 0.065 if off_kwh is an integer in (1, 5000), you found a solution.如果 off_kwh 是 (1, 5000) 中的 integer,您找到了解决方案。 that being said, the speed up is significant, but you will run into some numeric issues due to working with floating point numbers: you'd have to define some precision up to which level rounding errors are acceptable...话虽如此,速度提升很重要,但是由于使用浮点数,您会遇到一些数字问题:您必须定义一些精度,达到可以接受的舍入误差水平......

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

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