From the title I don't think you understand well in detail, I'm trying to check in the json file and check between the records sorted by the "Day" and "Championship" keys, check and calculate the time difference between them, obviously respecting the "Day" key and the "Championship" key, moreover I would like to better understand how to "NOT PRINT" and therefore discard the entire "Day" of that specific "Championship" if at least one match does not have the minimum time difference set by me, moreover I can't even to print the "Home" and "Away" keys, I would also like to better understand how to format the "datetime.timedelta" that I receive as a response into text. Thanks in advance to those who help me and give me all the possible explanations, you are fantastic!
file.json
[{
"Day": "Giornata 29",
"Matches": {
"Home": "Egnatia",
"Away": "Kukesi",
"Times": "19.03. 15:00",
"Championship": "CALCIO\nALBANIA Super League\n2023/2024"
}
},{
"Day": "Giornata 29",
"Matches": {
"Home": "Egnatia",
"Away": "Kukesi",
"Times": "20.03. 16:09",
"Championship": "CALCIO\nALBANIA Super League\n2023/2024"
}
},
{
"Day": "Giornata 29",
"Matches": {
"Home": "Egnatia",
"Away": "Kukesi",
"Times": "19.03. 16:15",
"Championship": "CALCIO\nALBANIA Super League\n2023/2024"
}
},{
"Day": "Giornata 41",
"Matches": {
"Home": "Lincoln",
"Away": "Leyton Orient",
"Times": "19.03. 16:00",
"Championship": "CALCIO\nINGHILTERRA League One\n2023/2024"
}
},
{
"Day": "Giornata 41",
"Matches": {
"Home": "Lincoln",
"Away": "Leyton Orient",
"Times": "19.03. 18:00",
"Championship": "CALCIO\nINGHILTERRA League One\n2023/2024"
}
},
{
"Day": "Giornata 30",
"Matches": {
"Home": "Napoli",
"Away": "Atalanta",
"Times": "30.03. 12:30",
"Championship": "CALCIO\nITALIA Serie A\n2023/2024"
}
}]
code.py
import collections
import datetime
import json
with open('file.json', 'r+') as f:
fileData = json.load(f)
# no f.close() is needed as the context manager handles this
# create a default dictionary with nested default dictionary that has a list in it
similar = collections.defaultdict(lambda : collections.defaultdict(list))
for file in fileData:
# for every file put it into the dictionary based on the day and the championship
similar[file["Day"]][file["Matches"]["Championship"]].append(file)
# make a dictionary from the defaultdict
championships = {k:dict(v) for k,v in similar.items()}
# loop over the daysuell
for day in championships:
# loop over the championships
for championship in championships.get(day):
# get the values
values = championships.get(day).get(championship)
# extract the times and parse the string to a timestamp and sort the list
times = sorted([datetime.datetime.strptime(val.get("Matches").get("Times"), "%d.%m. %H:%M") for val in values])
# calculate the time difference for every element and its adjacent element
# if the list has one or no elements return None
time_difference = None if len(times) <= 1 else list(map(lambda t: t[-1]-t[0], zip(times, times[1:])))
print(day, championship, time_difference, '\n\n')
**I expect a print like this: **
Giornata 29 CALCIO\nALBANIA Super League\n2023/2024 **2/3**
Egnatia - Kukesi v
Egnatia - Kukesi v
Egnatia - Kukesi x
---------------------------
Giornata 41 CALCIO\nINGHILTERRA League One\n2023/2024 **2/2**
Lincoln - Leyton Orient v
Lincoln - Leyton Orient v
---------------------------
Giornata 30 CALCIO\nITALIA Serie A\n2023/2024 **1/1**
Napoli - Atalanta
---------------------------
subsequently I should be able to print all the "Home" and "Away" that are within the requirements
Thank you so much, you are amazing!