I am trying to write a piece of code that appends the track['TrackConditionRating'] for all iterations, if the first iteration of 'RaceNumber' = 1. The problem with my current code is that is being applied broadly to all iterations. For example, it will not use track['TrackConditionRating'] if 'RaceNumber' = 6, even though the first iteration of 'RaceNumber' =1.
Example of JSON:
"TrackConditionHistory":[
{
"RaceNumber":1,
"TrackConditionRatingSourceNumber":8,
"TrackCondition":"Heavy",
"TrackConditionRating":"Heavy 8",
"TrackConditionRatingSource":8,
"TrackConditionSource":5
},
{
"RaceNumber":6,
"TrackConditionRatingSourceNumber":9,
"TrackCondition":"Heavy",
"TrackConditionRating":"Heavy 9",
"TrackConditionRatingSource":9,
"TrackConditionSource":5
}
]
My code:
for track in json['TrackConditionHistory']:
raceno = track['RaceNumber']
if not raceno == 1:
trackcondition = json['TrackCondition']
else:
trackcondition = track['TrackConditionRating']
data.append((meetcode, raceno, trackcondition))
CodePudding user response:
How to apply code to only first iteration of loop
I suggest using enumerate
for this task following way
for inx, track in enumerate(json['TrackConditionHistory']):
if inx == 0:
raceno = track['RaceNumber']
if not raceno == 1:
trackcondition = json['TrackCondition']
else:
trackcondition = track['TrackConditionRating']
This way raceno
will be changed solely in first turn of loop.
CodePudding user response:
While Daweo's answer works, if you have a big json file to parse you'll have to check that if condition for every loop iteration. What you can do instead is:
raceno = json['TrackConditionHistory'][0]['RaceNumber']
for track in json['TrackConditionHistory'][1:]:
if not raceno == 1:
trackcondition = json['TrackCondition']
else:
trackcondition = track['TrackConditionRating']
Also if raceno == 1
then you don't make use of the track
at all in the loop so there's not really a reason for it to be inside the loop. Simply use the loop when raceno == 1
, or don't use it at all.
As for the behavior type, you're only adding an entry to your data after the loop is completed, hence you only get the last values encountered in the loop.
So combining the initial race number check placed before the loop, and correcting the loop itself, we get the following full example:
test.json file:
{
"TrackConditionHistory":[
{
"RaceNumber":1,
"TrackConditionRatingSourceNumber":8,
"TrackCondition":"Heavy",
"TrackConditionRating":"Heavy 8",
"TrackConditionRatingSource":8,
"TrackConditionSource":5
},
{
"RaceNumber":6,
"TrackConditionRatingSourceNumber":9,
"TrackCondition":"Heavy",
"TrackConditionRating":"Heavy 9",
"TrackConditionRatingSource":9,
"TrackConditionSource":5
}
]
}
python file:
import json
data = []
with open('test.json', 'r') as f:
json = json.load(f)
firstraceno = json['TrackConditionHistory'][0]['RaceNumber']
if firstraceno == 1:
for track in json['TrackConditionHistory']:
raceno = track["RaceNumber"]
trackcondition = track["TrackConditionRating"]
data.append((raceno, trackcondition))
print(data)
Output:
[(1, 'Heavy 8'), (6, 'Heavy 9')]
Note that I don't what meetcode
is, and I had to complete the json sample to make it valid.