除以数字 - 多个级别 - 所有数字的总和等于输入数字 - Python

时间:2018-03-16 18:15:22

标签: python python-3.x

我如何划分多个级别的数字,我有一个数字和一个包含3个值的列表,第6步有预期的输出,但我无法得到它

Number = 500 
Divide=[5,6,9]

步骤1:

获取数字并除以分数列表中的数字总和

500/ (5+6+9) = 25

第2步:   将得到的数字乘以除数列表中的每个数字

25 *5 = 125 25* 6 = 150 25* 9 = 225

第3步:     打印我们在第2步中得到的数字和结果

500 125  
500 150  
500 225

第4步:     记下我们在第2步中得到的每一个数字,并将其除以如下

125 / (5+6+9) = 125/20 = 6.25

第5步

6.25 * 5 = 31.25
6.25 * 6 = 37.50
6.25 * 9 = 56.25

第6步: 从步骤3中取150和225并从步骤4到步骤6执行相同的操作并打印如下,这是最终输出,

500 125 31.25
500 125 37.50
500 125 56.25
500 150 37.5
500 150 45
500 150 67.5
500 225 56.25
500 225 67.5
500 225 101.25

为此,我可以使用以下代码获得第一级

number = 500
divide=[5,6,9]
for i in divide:
    j=(number/sum(divide))*i
    print (number, j)

输出:

500 125.0
500 150.0
500 225.0

如何继续获取步骤6中显示的下一级数字

**Expected Output 2**
    500 125 31.25
    500 125 68.75 #31.25+37.5
    500 125 125 # 68.75+56.25
    500 150 37.5
    500 150 82.5 #37.5+45
    500 150 150 # 67.5+82.5
    500 225 56.25
    500 225 123.75 #56.25+67.5
    500 225 225 # 123.75+101.25

输出3:

    500 125 31.25
    500 125 68.75 #31.25+37.5
    500 125 125 # 68.75+56.25
    500 150 45 # this is 2nd set and the multiplication should start from 2nd number in divide list 6 9 5
    500 150 112.5 #45+67.5
    500 150 150 # 112.5+.37.5
    500 225 101.25  # this is 3rd set and the multiplication should start from 3rd number in divide list 9 5 6
    500 225 157.5 #101.25+56.25
    500 225 225 # 157.5+67.5

2 个答案:

答案 0 :(得分:2)

您只需要一些嵌套循环和一个临时列表,用于第一次循环传递的结果。

number = 500
divide=[5,6,9]
interim = []
for i in divide:
    j=(number/sum(divide))*i
    interim.append(j)
    print (number, j)

for j in interim:
    for i in divide:
        k = j/sum(divide)*i
        print (j, k)

这为我返回了以下内容

500 125.0
500 150.0
500 225.0
125.0 31.25
125.0 37.5
125.0 56.25
150.0 37.5
150.0 45.0
150.0 67.5
225.0 56.25
225.0 67.5
225.0 101.25

答案 1 :(得分:1)

使用一些list理解,你可以达到你想要的效果。

通过每一步

分解代码
Number = 500 
Divide = [5,6,9]

step1 = Number/sum(Divide)

step2 = [step1 * i for i in Divide]

for i in step2:             # step3
    print('{} {}'.format(Number, i))

step4 = [i / sum(Divide) for i in step2]

step5 = [[i * j for i in step4] for j in Divide]

k = 0                       # step6
for i in step2:
    for j in step5[k]:
        print('{} {} {}'.format(Number, i, j))
    k += 1

输出:

500 125.0
500 150.0                                                   
500 225.0                                                   
500 125.0 31.25                                             
500 125.0 37.5                                              
500 125.0 56.25                                             
500 150.0 37.5                                              
500 150.0 45.0                                              
500 150.0 67.5                                              
500 225.0 56.25                                             
500 225.0 67.5                                              
500 225.0 101.25       

解决更新的问题:

Number = 500 
Divide = [5,6,9]

step1 = Number/sum(Divide)

step2 = [step1 * i for i in Divide]

for i in step2:             # step3
    print('{} {}'.format(Number, i))

step4 = [i / sum(Divide) for i in step2]

step5 = [[i * j for i in step4] for j in Divide]

k = 0                    # step6
for i in step2:
    sum = 0
    for j in step5[k]:
        sum = sum + j
        print('{} {} {}'.format(Number, i, sum))
    k += 1

输出(2):

500 125.0
500 150.0                                                   
500 225.0                                                   
500 125.0 31.25                                             
500 125.0 68.75                                             
500 125.0 125.0                                             
500 150.0 37.5                                              
500 150.0 82.5                                              
500 150.0 150.0                                             
500 225.0 56.25                                             
500 225.0 123.75                                            
500 225.0 225.0