一段时间以来,我一直在用类似的标签创建这些列表,例如“March_01_2017”,“March_02_2017”等等,这些列表基本上是过时的序列。
而不必重复键入列表显示,如:
>>>Date=[]
我宁愿将参数传递给函数并让它为我完成工作。我现在的问题是,我不知道如何获得执行此任务的功能,或者知道我需要阅读以了解如何执行此操作。
为什么不用字典?
import datetime
dict_of_lists = {}
def add_list():
date = datetime.datetime.now().strftime("%B_%d_%Y")
if date not in dict_of_lists:
dict_of_lists[date] = []
else:
print("Key already exists")
现在,您可以每天拨打add_list()
一次,创建一个包含所需标签的新列表。
样本输出:
In [51]: dict_of_lists
Out[51]: {}
In [52]: add_list()
In [53]: dict_of_lists
Out[53]: {'March_02_2018': []}
In [54]: add_list()
Key already exists