我有大量的体育数据,我正在分析。我正在试图找到团队的最后一个位置(时区)。
我决定创建一本词典是一个可行的选择。
这是我到目前为止得到的,输出游戏数据和时区。
schedule_ANA=dict()
#ANA is the team I'm interested in
if str(hometeam) == "ANA" or str(awayteam) == "ANA":
schedule_ANA[date]=str(timezone)
# This finds the time zone of the game on that date
print (schedule_ANA)这是输出
{'2018/09/08': 'GMT-5'}
{'2018/09/09': 'GMT-5'}
{'2018/08/03': 'GMT-4'}
{'2018/08/04': 'GMT-4'}
{'2018/08/05': 'GMT-4'}
{'2018/05/08': 'GMT-6'}
{'2018/05/09': 'GMT-6'}
{'2018/05/28': 'GMT-4'}
{'2018/05/29': 'GMT-4'}
{'2018/05/30': 'GMT-4'}
{'2018/05/31': 'GMT-4'}
{'2018/04/23': 'GMT-5'}
{'2018/04/24': 'GMT-5'}
{'2018/08/28': 'GMT-7'}
{'2018/04/25': 'GMT-5'}
{'2018/09/01': 'GMT-5'}现在我需要考虑从那个日期开始的前一个时区。
我想找到最后一场比赛的时区。我想回去一天一次,直到下一个游戏日期/时区出现。
例如,如果一个游戏是在2018年8月28日在格林尼治时间-7,没有游戏在29,30或31日,那么另一个游戏在2018年9月1日在格林尼治时间-5,我想要一个函数返回格林尼治时间-7,当2018年9月1日是输入。
我想用if语句说
if date == 2018/09/01
previous_date=date-1
previous_timezone= schedule_ANA[previous_date]我不确定以下情况。1.如果这是字典的正确语法,2.如何将日期转换成格式,这样才能工作(回到前一个月的1天)
提前感谢!
-
-
编辑谢谢大家的评论。我正在努力实现答案中的一些解决方案。这是我更新的代码,但我仍然收到一个错误。
schedule_ANA=dict()
#ANA is the team I'm interested in
if str(hometeam) == "ANA" or str(awayteam) == "ANA":
schedule_ANA[date]=str(timezone)
# This finds the time zone of the game on that date
print (schedule_ANA)
from datetime import datetime
from ast import literal_eval
schedule_ANA = [literal_eval(i) for i in schedule_ANA]
sorted_dates = sorted([(k,v)for i in schedule_ANA for k,v in i.items()], key=lambda x: datetime.strptime(x[0], "%Y/%m/%d"))误差
Traceback (most recent call last):
File "H:/2019sem2/egh400/Code/rev40.py", line 147, in <module>
schedule_ANA = [literal_eval(i) for i in schedule_ANA]
File "H:/2019sem2/egh400/Code/rev40.py", line 147, in <listcomp>
schedule_ANA = [literal_eval(i) for i in schedule_ANA]
File "C:\Program Files\IBM\SPSS 25\Python3\lib\ast.py", line 46, in literal_eval
node_or_string = parse(node_or_string, mode='eval')
File "C:\Program Files\IBM\SPSS 25\Python3\lib\ast.py", line 35, in parse
return compile(source, filename, mode, PyCF_ONLY_AST)
File "<unknown>", line 1
2018/04/03
^
SyntaxError: invalid token发布于 2019-09-20 05:09:09
@shaik moeed指出。用一张清单。
def get_last_date(date_str):
return [x for x in schedule_ANA.keys()].index(date_str) - 1 如果date_str不在schedule_ANA dict中,上述代码将给出值错误。
def get_last_date(date_str):
return next((x for x in schedule_ANA.keys()), [-1]) - 1如果date_str不在dict中,上述代码的默认值为-1。
发布于 2019-09-20 05:15:33
我已将你的字典转换为元组列表,并按日期排序。
>>> from datetime import datetime
>>> dates_list
[{'2018/09/08': 'GMT-5'}, {'2018/09/09': 'GMT-5'}, {'2018/08/03': 'GMT-4'}, {'2018/08/04': 'GMT-4'}, {'2018/08/05': 'GMT-4'}, {'2018/05/08': 'GMT-6'}, {'2018/05/09': 'GMT-6'}, {'2018/05/28': 'GMT-4'}, {'2018/05/29': 'GMT-4'}, {'2018/05/30': 'GMT-4'}, {'2018/05/31': 'GMT-4'}, {'2018/04/23': 'GMT-5'}, {'2018/04/24': 'GMT-5'}, {'2018/08/28': 'GMT-7'}, {'2018/04/25': 'GMT-5'}, {'2018/09/01': 'GMT-5'}]
# if elements are in string, to convert them to actual data type, here dictionary,
# follow below steps
# >>> from ast import literal_eval
# >>> dates_list = [literal_eval(i) for i in dates_list]
>>> sorted_dates = sorted([(k,v)for i in dates_list for k,v in i.items()], key=lambda x: datetime.strptime(x[0], "%Y/%m/%d"))
[('2018/04/23', 'GMT-5'), ('2018/04/24', 'GMT-5'), ('2018/04/25', 'GMT-5'), ('2018/05/08', 'GMT-6'), ('2018/05/09', 'GMT-6'), ('2018/05/28', 'GMT-4'), ('2018/05/29', 'GMT-4'), ('2018/05/30', 'GMT-4'), ('2018/05/31', 'GMT-4'), ('2018/08/03', 'GMT-4'), ('2018/08/04', 'GMT-4'), ('2018/08/05', 'GMT-4'), ('2018/08/28', 'GMT-7'), ('2018/09/01', 'GMT-5'), ('2018/09/08', 'GMT-5'), ('2018/09/09', 'GMT-5')]现在,您可以通过使用索引值来检查以前的日期。
https://stackoverflow.com/questions/58021935
复制相似问题