Код: Выделить всё
# Sample Code
import datetime
sample = {
"Eat": [
datetime.datetime(2024, 9, 13, 2, 0),
datetime.datetime(2024, 9, 13, 3, 0),
"2024-09-13 20:43:35.440898",
],
"Sleep": [
datetime.datetime(2024, 9, 13, 4, 0),
datetime.datetime(2024, 9, 13, 20, 0),
"2024-09-13 20:43:24.354440",
],
}
# This is the entire function that I am using
def SortObjectives(obj):
temLis = []
temDic = {}
for i in obj:
print(str(obj[i][0].time()))
temLis.append(obj[i][0].time())
temLis.sort()
print(temLis)
temCou = 0
# Recursive Function
def SortObjectivesR(count):
for i in obj:
print(count)
print(str(temLis[count]) + " --From temLis)")
if obj[i][0].time() == temLis[count]:
temDic[i] = obj[i]
print(len(temDic))
print(len(obj))
if len(temDic) == len(obj):
print("Here")
print(temDic)
return temDic
# Why does this return None when it has a literal value???
# Keep in mind that this function only does this when their are two items
# in the obj dictionary when their not in order
# It works when in order and when in the number of items is > or < then 2
else:
count += 1
SortObjectivesR(count)
# return temDic - not part of original code
print(SortObjectivesR(temCou))
# How I find what it returns
# Would put in return statement to save lines, but it returns None
# Under the specific condition mentioned, the alteration of
# Variables work just fine.
return temDic
print(SortObjectives(sample))
Комментарии, которые можно пропустить:
Почему эта функция возвращает None, если она имеет буквальное значение???
Имейте в виду, что эта функция делает это только тогда, когда это два элемента
в словаре obj, когда их нет в порядке
Он работает, когда в порядке и когда количество элементов больше или меньше 2
БЫЛО бы вставить оператор return для сохранения строк, но он возвращает None
При указанном конкретном условии , изменение
переменных работает нормально.
Подробнее здесь: https://stackoverflow.com/questions/789 ... n-a-functi