简体   繁体   English

是否可以在同一个循环中再次检查列表中的索引? python

[英]is it possible to recheck an index in a list in the same loop again? python

What I'm trying to do is to make the loop goes to every index in the "investments" list then checks if it's equal to "aMoney" variable or not.我想要做的是让循环转到“投资”列表中的每个索引,然后检查它是否等于“aMoney”变量。 if the statement is True then the same index of "investments" in "revenue" list data will be added to "totalMoney" variable and goes back again to do next.如果语句为真,则“收入”列表数据中“投资”的相同索引将添加到“totalMoney”变量并再次返回以执行下一步。 My problem is that I want the loop to go back to the False statements to recheck if it's True again and add it to the "totalMoney" variable.我的问题是我希望循环到 go 返回到 False 语句以重新检查它是否再次为 True 并将其添加到“totalMoney”变量。

what's happening here is the loop will skip index[0] because 2040 >=/ 3000. but after many loops the condition will be True if we do it again with the new number.这里发生的是循环将跳过索引 [0],因为 2040 >=/ 3000。但是在许多循环之后,如果我们用新数字再次执行,条件将为 True。

note: Sorting won't work because index[] in first list must go with what same index[] in the second list, no changes.注意:排序将不起作用,因为第一个列表中的 index[] 必须与第二个列表中的相同 index[] 为 go,没有变化。

here is my full code:这是我的完整代码:

numOfProjects = int(7) 
aMoney = int(2040)

investments = [3000,2040,3040,5000,3340,4000,7000]
revenue = [500,1000,300,450,2010,650,1500]

totalMoney = int()
totalMoney = aMoney
for j in range(len(investments)):
    if(totalMoney >= investments[j]):
        totalMoney += revenue[j]
        investments[j] + 1
        revenue[j] + 1
    
totalMoney -= aMoney 
print("The profit is $", totalMoney)

the output of this code will be $3960此代码的 output 将是 $3960

in papers, it should be $4910 because, first round should be "2040 + 1000 + 300 + 2010 + 650" = 6000 then we go back at what we left "6000 + 500 + 450" = 6950 6950 - 2040 = 4910 I tried my best explaining the idea, hope it's clear在论文中,它应该是 4910 美元,因为,第一轮应该是“2040 + 1000 + 300 + 2010 + 650”= 6000 然后我们 go 回到我们留下的“6000 + 500 + 450”= 6950 6950 - 2040 = 4910 我试过我最好解释这个想法,希望它很清楚

In python, we don't need to initialise int variables as int(number) , just declaring them with the corresponding value suffices.在 python 中,我们不需要将 int 变量初始化为int(number) ,只需将它们声明为相应的值即可。 So instead of aMoney = int(2040) , just aMoney = 2040 works.因此,代替aMoney = int(2040) ,只需aMoney = 2040

You can use zip to sort the investments and revenue together.您可以使用zipinvestmentsrevenue一起排序。 Here's a simplified code which does what you need -这是一个简化的代码,可以满足您的需要 -

initialMoney, currentMoney = 2040, 2040
investments = [3000,2040,3040,5000,3340,4000,7000]
revenue = [500,1000,300,450,2010,650,1500]

data = sorted(zip(investments, revenue))
print(data)

for investment, revenue in data:
    if investment <= currentMoney:
        currentMoney += revenue

netProfit = currentMoney - initialMoney
print("The profit is $", netProfit)

Output: Output:

[(2040, 1000), (3000, 500), (3040, 300), (3340, 2010), (4000, 650), (5000, 450), (7000, 1500)]
The profit is $ 4910

What you see printed just before profit, is the (investment, revenue) sorted by investment.您在利润之前看到的是按投资排序的(investment, revenue)

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

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