Python: “casser” si l'instruction à l'intérieur d'une boucle for
Je comprends qu'on ne peut pas "casser" une instruction si et seulement à partir d'une boucle, cependant, je vais essayer d'conceptuellement arrêter une instruction if de l'évaluation d'après ce qu'il trouve un "vrai" la première fois, quand il est à l'intérieur d'une boucle 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])
Il y a deux clés dans la masterDictionary et chacun d'eux contient une liste de 27 et 29 points de données, respectivement. Par conséquent, la boucle
for i in range(len(diceList)):
sera exécuté i de 0 - 26 et 0 - 28 pour chaque clé. C'est super, mais le problème lorsque l'instruction if est évalué, est qu'une fois qu'il est trouvé, il sera par la suite être vrai pour l'ensemble de la gamme suivante des éléments. Voici la sortie d'impression:
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
Partout où il y a un " X " signifie que si la déclaration était fausse, et une fois un 'O' commence, le reste des instructions sera toujours vrai en raison de l'augmentation de la taille du problème conteneur (jusqu'à 1,0).
Ce que je cherche, c'est une façon de dire à mon instruction if dans la boucle pour arrêter une fois qu'il trouve la première vraie déclaration, puis écrire le dictionnaire, puis continuer la boucle externe de nouveau.
Toute aide appréciée!
Mise à JOUR:
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
produit:
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]}
OriginalL'auteur A.Krueger | 2011-08-11
Vous devez vous connecter pour publier un commentaire.
Pause mettra fin à "la plus proche de la boucle englobante, en sautant, en option clause else si la boucle a un." La boucle externe va juste continuer avec la prochaine itération. Si vous n'êtes pas "casser la si", mais la boucle, le cas est clos. Avant la pause, il vous suffit de définir toutes les valeurs de
diceList[0:i+1]
àdiceList[0:len(diceList)+1]
de vrai.La programmation orientée objet, tant pis. J'ai déménagé thisList en dehors de la boucle et qu'il s'est arrêté à partir de l'écrasement. La surveillance de ma part. Merci!
OriginalL'auteur Jacob
Une méthode est de soulever une exception à l'intérieur du code, et de l'attraper à l'intérieur de la boucle et continue la boucle.
OriginalL'auteur Keith
Alors, quelles sont les conditions que vous savez la déclaration est
True
? J'ai pensé qu'il pourrait être "si la dernière instruction était Vrai", mais dans votre exemple de sortie que vous avez finalement revenir àFalse
?De toute façon, envisager d'ajouter ce que une sorte de première condition à votre si:
Si la première partie est évaluée comme Fausse (c'est à dire que vous FAITES déjà sais que c'est
True
), Python ne devrait pas évaluer le second terme de laand
(depuis maintenant ne peut pas êtreTrue
) et déplacer le long. Vous souhaitez simplement besoin d'ajouter une autre clause else (et donc peut-être faire laelse
unelif
) et de les traiter.NB: de Cette façon, peut-être un peu hacky en fonction de ce que vous devez faire afin de déterminer si vous connaissez déjà la déclaration est
True
:\OriginalL'auteur bdeniker
De ce que je comprends, vous êtes en essayant de sauver la si la comparaison pour le reste de la boucle. Je pense que vous devez vous séparer de la boucle que vous avez en deux boucles, l'une qui ne l'instruction if et trouve la partition, puis la seconde, tout simplement pas de comparaison. Vous pouvez garder le courant i vous travaillez sur une variable externe à la boucle, et puis continuer à partir de ce point dans la deuxième boucle.
OriginalL'auteur Kenji