在以日期时间为键的字典中使用 iteritems
Use iteritems in a dictionary with datetime as key
我有一个字典 update_fields
,它有 key/value 对,其中值是另一个字典:
{datetime.date(2016, 12, 2): {'t1030': 0, 't1045': 0, 't0645': 0, 't1645': 0, 't0600': 0, 't1415': 0, 't1000': 0, 't1430': 0, 't0700': 0, 't1800': 0, 't1715': 0, 't1630': 0, 't1615': 0, 't1945': 0, 't1730': 0, 't1530': 0, 't1515': 0, 't0830': 0, 't0915': 0, 't1245': 0, 't1300': 0, 't1600': 0, 't1900': 0, 't2000': 0, 't2115': 0, 't0715': 0}, datetime.date(2016, 12, 1): {'t1030': 0, 't1045': 0, 't0645': 0, 't1645': 0, 't0600': 0, 't1415': 0, 't1000': 0, 't1430': 0, 't0700': 0, 't1800': 0, 't1715': 0, 't1630': 0, 't1615': 0, 't1945': 0, 't1730': 0, 't1530': 0, 't1515': 0, 't0830': 0, 't0915': 0, 't1245': 0, 't1300': 0, 't1600': 0, 't1900': 0, 't2000': 0, 't2115': 0, 't0715': 0}}
我想从每个键值创建另一个字典(或以某种方式按原样提取它),但是当我尝试这样做时:
for update_date in update_fields:
timeslot_fields = {timeslot: value for (timeslot, value) in update_date.iteritems()}
我得到AttributeError: 'datetime.date' object has no attribute 'iteritems'
当我这样尝试时:
for update_date, values in update_fields:
timeslot_fields = {timeslot: value for (timeslot, value) in values.iteritems()}
我得到TypeError: 'datetime.date' object is not iterable
我做错了什么?它可能与外部字典键是日期时间这一事实有关吗?无论我尝试什么,我似乎都无法破解密钥并访问它的值。
这是因为您正在尝试迭代密钥。
for update_date in update_fields:
items = update_fields[update_date].items()
timeslot_fields = {timeslot: value for (timeslot, value) in items}
当您遍历 Python 中的字典时,默认情况下,您将遍历键。如果您想遍历值,请尝试 update_fields.values()
或者 update_fields.itervalues()
for update_date in update_fields.itervalues():
timeslot_fields = {timeslot: value for (timeslot, value) in update_date.iteritems()}
如果你想迭代项目,你应该使用 update_fields.items()
或 update_fields.iteritems()
for update_date, values in update_fields.iteritems():
timeslot_fields = {timeslot: value for (timeslot, value) in values.iteritems()}
将 update_date.iteritems()
更改为 update_fields[update_date].iteritems()
、
for update_date in update_fields:
timeslot_fields = {timeslot: value for (timeslot, value) in update_fields[update_date].iteritems()}
我有一个字典 update_fields
,它有 key/value 对,其中值是另一个字典:
{datetime.date(2016, 12, 2): {'t1030': 0, 't1045': 0, 't0645': 0, 't1645': 0, 't0600': 0, 't1415': 0, 't1000': 0, 't1430': 0, 't0700': 0, 't1800': 0, 't1715': 0, 't1630': 0, 't1615': 0, 't1945': 0, 't1730': 0, 't1530': 0, 't1515': 0, 't0830': 0, 't0915': 0, 't1245': 0, 't1300': 0, 't1600': 0, 't1900': 0, 't2000': 0, 't2115': 0, 't0715': 0}, datetime.date(2016, 12, 1): {'t1030': 0, 't1045': 0, 't0645': 0, 't1645': 0, 't0600': 0, 't1415': 0, 't1000': 0, 't1430': 0, 't0700': 0, 't1800': 0, 't1715': 0, 't1630': 0, 't1615': 0, 't1945': 0, 't1730': 0, 't1530': 0, 't1515': 0, 't0830': 0, 't0915': 0, 't1245': 0, 't1300': 0, 't1600': 0, 't1900': 0, 't2000': 0, 't2115': 0, 't0715': 0}}
我想从每个键值创建另一个字典(或以某种方式按原样提取它),但是当我尝试这样做时:
for update_date in update_fields:
timeslot_fields = {timeslot: value for (timeslot, value) in update_date.iteritems()}
我得到AttributeError: 'datetime.date' object has no attribute 'iteritems'
当我这样尝试时:
for update_date, values in update_fields:
timeslot_fields = {timeslot: value for (timeslot, value) in values.iteritems()}
我得到TypeError: 'datetime.date' object is not iterable
我做错了什么?它可能与外部字典键是日期时间这一事实有关吗?无论我尝试什么,我似乎都无法破解密钥并访问它的值。
这是因为您正在尝试迭代密钥。
for update_date in update_fields:
items = update_fields[update_date].items()
timeslot_fields = {timeslot: value for (timeslot, value) in items}
当您遍历 Python 中的字典时,默认情况下,您将遍历键。如果您想遍历值,请尝试 update_fields.values()
或者 update_fields.itervalues()
for update_date in update_fields.itervalues():
timeslot_fields = {timeslot: value for (timeslot, value) in update_date.iteritems()}
如果你想迭代项目,你应该使用 update_fields.items()
或 update_fields.iteritems()
for update_date, values in update_fields.iteritems():
timeslot_fields = {timeslot: value for (timeslot, value) in values.iteritems()}
将 update_date.iteritems()
更改为 update_fields[update_date].iteritems()
、
for update_date in update_fields:
timeslot_fields = {timeslot: value for (timeslot, value) in update_fields[update_date].iteritems()}