[英]Python: "breaking out" of if statement inside a for loop
我知道一個 if 語句只能從循環中“中斷”,但是,我試圖從概念上阻止 if 語句在它第一次在 for 循環內找到“真”后進行評估。
# Import XML Parser
import xml.etree.ElementTree as ET
# Parse XML directly from the file path
tree = ET.parse('xml file')
# Create iterable item list
items = tree.findall('item')
# Create class for historic variables
class DataPoint:
def __init__(self, low, high, freq):
self.low = low
self.high = high
self.freq = freq
# Create Master Dictionary and variable list for historic variables
masterDictionary = {}
# Loop to assign variables as dictionary keys and associate their values with them
for item in items:
thisKey = item.find('variable').text
thisList = []
masterDictionary[thisKey] = thisList
for item in items:
thisKey = item.find('variable').text
newDataPoint = DataPoint(float(item.find('low').text), float(item.find('high').text), float(item.find('freq').text))
masterDictionary[thisKey].append(newDataPoint)
diceDictionary = {}
import random
for thisKey in masterDictionary.keys():
randomValue = random.random()
diceList = []
thisList = []
diceList = masterDictionary[thisKey]
diceDictionary[thisKey] = thisList
for i in range(len(diceList)):
if randomValue <= sum(i.freq for i in diceList[0:i+1]):
print 'O', i, 'randomValue', randomValue, 'prob container', sum(i.freq for i in diceList[0:i+1])
#diceRoll = random.uniform(diceList[i].low, diceList[i].high)
#diceDictionary[thisKey].append(diceRoll)
else:
print 'X', i, 'randomValue', randomValue, 'prob container', sum(i.freq for i in diceList[0:i+1])
masterDictionary 中有兩個鍵,每個鍵分別包含 27 和 29 個數據點的列表。 因此,循環
for i in range(len(diceList)):
將為每個鍵從 0 - 26 和 0 - 28 運行 i 。 這很好,但是在評估 if 語句時的問題是,一旦找到它,隨后對所有以下范圍項都將成立。 這是打印 output:
X 0 randomValue 0.0775612781213 prob container 0.0294117647059
X 1 randomValue 0.0775612781213 prob container 0.0294117647059
X 2 randomValue 0.0775612781213 prob container 0.0294117647059
X 3 randomValue 0.0775612781213 prob container 0.0294117647059
O 4 randomValue 0.0775612781213 prob container 0.147058823529
O 5 randomValue 0.0775612781213 prob container 0.235294117647
O 6 randomValue 0.0775612781213 prob container 0.441176470588
O 7 randomValue 0.0775612781213 prob container 0.588235294118
O 8 randomValue 0.0775612781213 prob container 0.676470588235
O 9 randomValue 0.0775612781213 prob container 0.764705882353
O 10 randomValue 0.0775612781213 prob container 0.794117647059
O 11 randomValue 0.0775612781213 prob container 0.823529411765
O 12 randomValue 0.0775612781213 prob container 0.823529411765
O 13 randomValue 0.0775612781213 prob container 0.852941176471
O 14 randomValue 0.0775612781213 prob container 0.882352941176
O 15 randomValue 0.0775612781213 prob container 0.882352941176
O 16 randomValue 0.0775612781213 prob container 0.911764705882
O 17 randomValue 0.0775612781213 prob container 0.911764705882
O 18 randomValue 0.0775612781213 prob container 0.911764705882
O 19 randomValue 0.0775612781213 prob container 0.911764705882
O 20 randomValue 0.0775612781213 prob container 0.911764705882
O 21 randomValue 0.0775612781213 prob container 0.941176470588
O 22 randomValue 0.0775612781213 prob container 0.941176470588
O 23 randomValue 0.0775612781213 prob container 0.970588235294
O 24 randomValue 0.0775612781213 prob container 0.970588235294
O 25 randomValue 0.0775612781213 prob container 0.970588235294
O 26 randomValue 0.0775612781213 prob container 0.970588235294
O 27 randomValue 0.0775612781213 prob container 0.970588235294
O 28 randomValue 0.0775612781213 prob container 1.0
X 0 randomValue 0.803308376497 prob container 0.0294117647059
X 1 randomValue 0.803308376497 prob container 0.0294117647059
X 2 randomValue 0.803308376497 prob container 0.0294117647059
X 3 randomValue 0.803308376497 prob container 0.0294117647059
X 4 randomValue 0.803308376497 prob container 0.0294117647059
X 5 randomValue 0.803308376497 prob container 0.0294117647059
X 6 randomValue 0.803308376497 prob container 0.0882352941176
X 7 randomValue 0.803308376497 prob container 0.0882352941176
X 8 randomValue 0.803308376497 prob container 0.0882352941176
X 9 randomValue 0.803308376497 prob container 0.117647058824
X 10 randomValue 0.803308376497 prob container 0.147058823529
X 11 randomValue 0.803308376497 prob container 0.205882352941
X 12 randomValue 0.803308376497 prob container 0.264705882353
X 13 randomValue 0.803308376497 prob container 0.294117647059
X 14 randomValue 0.803308376497 prob container 0.382352941176
X 15 randomValue 0.803308376497 prob container 0.441176470588
X 16 randomValue 0.803308376497 prob container 0.470588235294
X 17 randomValue 0.803308376497 prob container 0.470588235294
X 18 randomValue 0.803308376497 prob container 0.529411764706
X 19 randomValue 0.803308376497 prob container 0.588235294118
X 20 randomValue 0.803308376497 prob container 0.647058823529
X 21 randomValue 0.803308376497 prob container 0.764705882353
O 22 randomValue 0.803308376497 prob container 0.823529411765
O 23 randomValue 0.803308376497 prob container 0.882352941176
O 24 randomValue 0.803308376497 prob container 0.970588235294
O 25 randomValue 0.803308376497 prob container 0.970588235294
O 26 randomValue 0.803308376497 prob container 1.0
任何有“X”的地方都意味着 if 語句為假,一旦“O”開始,語句的 rest 將始終為真,因為 prob 容器的大小不斷增加(最高為 1.0)。
我正在尋找的是一種方法來告訴循環內的 if 語句一旦找到第一個真正的語句就停止,然后寫入字典,然后再次繼續外部循環。
任何幫助表示贊賞!
更新:
diceDictionary = {}
x=0
while x < 3:
import random
for thisKey in masterDictionary.keys():
randomValue = random.random()
diceList = []
thisList = []
diceList = masterDictionary[thisKey]
diceDictionary[thisKey] = thisList
for i in range(len(diceList)):
if randomValue <= sum(i.freq for i in diceList[0:i+1]):
print 'O', thisKey, i, 'randomValue', randomValue, 'prob container', sum(i.freq for i in diceList[0:i+1])
diceRoll = random.uniform(diceList[i].low, diceList[i].high)
diceDictionary[thisKey].append(diceRoll)
break
else:
print 'X', thisKey, i, 'randomValue', randomValue, 'prob container', sum(i.freq for i in diceList[0:i+1])
x = x + 1
print diceDictionary
產生:
X inflation 0 randomValue 0.500605733928 prob container 0.0294117647059
X inflation 1 randomValue 0.500605733928 prob container 0.0294117647059
X inflation 2 randomValue 0.500605733928 prob container 0.0294117647059
X inflation 3 randomValue 0.500605733928 prob container 0.0294117647059
X inflation 4 randomValue 0.500605733928 prob container 0.147058823529
X inflation 5 randomValue 0.500605733928 prob container 0.235294117647
X inflation 6 randomValue 0.500605733928 prob container 0.441176470588
O inflation 7 randomValue 0.500605733928 prob container 0.588235294118
X stock 0 randomValue 0.392225720409 prob container 0.0294117647059
X stock 1 randomValue 0.392225720409 prob container 0.0294117647059
X stock 2 randomValue 0.392225720409 prob container 0.0294117647059
X stock 3 randomValue 0.392225720409 prob container 0.0294117647059
X stock 4 randomValue 0.392225720409 prob container 0.0294117647059
X stock 5 randomValue 0.392225720409 prob container 0.0294117647059
X stock 6 randomValue 0.392225720409 prob container 0.0882352941176
X stock 7 randomValue 0.392225720409 prob container 0.0882352941176
X stock 8 randomValue 0.392225720409 prob container 0.0882352941176
X stock 9 randomValue 0.392225720409 prob container 0.117647058824
X stock 10 randomValue 0.392225720409 prob container 0.147058823529
X stock 11 randomValue 0.392225720409 prob container 0.205882352941
X stock 12 randomValue 0.392225720409 prob container 0.264705882353
X stock 13 randomValue 0.392225720409 prob container 0.294117647059
X stock 14 randomValue 0.392225720409 prob container 0.382352941176
O stock 15 randomValue 0.392225720409 prob container 0.441176470588
X inflation 0 randomValue 0.146182475695 prob container 0.0294117647059
X inflation 1 randomValue 0.146182475695 prob container 0.0294117647059
X inflation 2 randomValue 0.146182475695 prob container 0.0294117647059
X inflation 3 randomValue 0.146182475695 prob container 0.0294117647059
O inflation 4 randomValue 0.146182475695 prob container 0.147058823529
X stock 0 randomValue 0.745100497977 prob container 0.0294117647059
X stock 1 randomValue 0.745100497977 prob container 0.0294117647059
X stock 2 randomValue 0.745100497977 prob container 0.0294117647059
X stock 3 randomValue 0.745100497977 prob container 0.0294117647059
X stock 4 randomValue 0.745100497977 prob container 0.0294117647059
X stock 5 randomValue 0.745100497977 prob container 0.0294117647059
X stock 6 randomValue 0.745100497977 prob container 0.0882352941176
X stock 7 randomValue 0.745100497977 prob container 0.0882352941176
X stock 8 randomValue 0.745100497977 prob container 0.0882352941176
X stock 9 randomValue 0.745100497977 prob container 0.117647058824
X stock 10 randomValue 0.745100497977 prob container 0.147058823529
X stock 11 randomValue 0.745100497977 prob container 0.205882352941
X stock 12 randomValue 0.745100497977 prob container 0.264705882353
X stock 13 randomValue 0.745100497977 prob container 0.294117647059
X stock 14 randomValue 0.745100497977 prob container 0.382352941176
X stock 15 randomValue 0.745100497977 prob container 0.441176470588
X stock 16 randomValue 0.745100497977 prob container 0.470588235294
X stock 17 randomValue 0.745100497977 prob container 0.470588235294
X stock 18 randomValue 0.745100497977 prob container 0.529411764706
X stock 19 randomValue 0.745100497977 prob container 0.588235294118
X stock 20 randomValue 0.745100497977 prob container 0.647058823529
O stock 21 randomValue 0.745100497977 prob container 0.764705882353
X inflation 0 randomValue 0.332170052306 prob container 0.0294117647059
X inflation 1 randomValue 0.332170052306 prob container 0.0294117647059
X inflation 2 randomValue 0.332170052306 prob container 0.0294117647059
X inflation 3 randomValue 0.332170052306 prob container 0.0294117647059
X inflation 4 randomValue 0.332170052306 prob container 0.147058823529
X inflation 5 randomValue 0.332170052306 prob container 0.235294117647
O inflation 6 randomValue 0.332170052306 prob container 0.441176470588
X stock 0 randomValue 0.145551106438 prob container 0.0294117647059
X stock 1 randomValue 0.145551106438 prob container 0.0294117647059
X stock 2 randomValue 0.145551106438 prob container 0.0294117647059
X stock 3 randomValue 0.145551106438 prob container 0.0294117647059
X stock 4 randomValue 0.145551106438 prob container 0.0294117647059
X stock 5 randomValue 0.145551106438 prob container 0.0294117647059
X stock 6 randomValue 0.145551106438 prob container 0.0882352941176
X stock 7 randomValue 0.145551106438 prob container 0.0882352941176
X stock 8 randomValue 0.145551106438 prob container 0.0882352941176
X stock 9 randomValue 0.145551106438 prob container 0.117647058824
O stock 10 randomValue 0.145551106438 prob container 0.147058823529
{'inflation': [0.028073642645577577], 'stock': [-0.07388514885974767]}
if randomValue <= sum(i.freq for i in diceList[0:i+1]):
print 'O', i, 'randomValue', randomValue, 'prob container', sum(i.freq for i in diceList[0:i+1])
break
Break將終止“最近的封閉循環,如果循環有一個,則跳過可選的 else 子句”。 外循環將繼續下一次迭代。 所以你不是“打破 if”,而是 if 被包含在循環中。在 break 之前,你可以將diceList[0:i+1]
到diceList[0:len(diceList)+1]
的所有值設置為真的。
一種方法是在內部代碼中引發異常,並在 for 循環中捕獲它並繼續循環。
那么,您知道該語句為True
的條件是什么? 我認為這可能是“如果最后一個語句是 True”,但是在您的示例 output 您最終 go 回到False
?
無論哪種方式,請考慮將此作為某種第一個條件添加到您的 if:
if (you don't already know it's True) and (the condition you currently evaluate):
<Do calculations>
如果第一部分被評估為 False (即您已經知道它是True
),Python 不應該評估and
的第二項(因為它現在不能是True
)並繼續前進。 您只需要添加另一個 else 子句(因此可能使else
成為elif
)並在那里處理它。
注意:這種方式可能有點麻煩,具體取決於您需要做什么以確定您是否已經知道該語句是True
:\
如果您確定要跳出 if 語句代碼塊,並且如果只有“break”有效,您可以將“break”放在您想要的位置,然后將整個 if 語句塊放在循環一次迭代。
據我了解,您正在嘗試為循環的其余部分保存 if 比較。 我認為你必須將你擁有的循環分成兩個循環,一個執行 if 語句並找到分區點,然后第二個只是跳過比較。 您可以將當前正在處理的 i 保留在循環外部的變量上,然后在第二個循環中從該點繼續。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.