【发布时间】:2016-01-19 09:21:33
【问题描述】:
在以下示例中,我将数据包含在字典中。应用一些代码后,正确的输出应该是值 905。然而,我当前的代码产生的值是 1296,这是不正确的。
变量 non_udacity_enollments 看起来不错,因为它提供的输出与我正在学习的练习代码相同。
下面的代码是我正在学习的练习代码的改编。
non_udacity_enrollments=[]
non_udacity_enrollments[:] = [v for v in enrollments if v.get('is_udacity')=='False']
paid_students = {}
for enrollment in non_udacity_enrollments:
if (not enrollment['is_canceled'] or
enrollment['days_to_cancel'] > 7):
account_key = enrollment['account_key']
enrollment_date = enrollment['join_date']
if (account_key not in paid_students or
enrollment_date > paid_students[account_key]):
paid_students[account_key] = enrollment_date
len(paid_students)
我试图了解我哪里出错了。谢谢。
根据要求,这是打印注册[0]
的输出{u'status': u'canceled', u'is_udacity': u'True', u'is_canceled': u'True', u'join_date': u'2014-11-10', u' account_key': u'448', u'cancel_date': u'2015-01-14', u'days_to_cancel': u'65'}
以及printpaid_students
的输出{u'199': u'2015-05-30', u'593': u'2015-04-02', u'1200': u'2015-03-04', u'1175' : u'2015-04-02', u'1269': u'2015-08-21', u'1268': u'2015-04-06', u'1256': u'2015-07-18 ', u'669': u'2015-05-12', u'1257': u'2015-07-09', u'1145': u'2015-04-04', u'344': u '2015-01-11', u'345': u'2015-01-07', u'346': u'2014-12-08', u'347': u'2015-04-05', u'340':u'2015-04-01',u'341':u'2015-05-10',u'342':u'2014-12-05',u'343':u'2014 -12-07', u'810': u'2014-11-10', u'919': u'2014-11-10', u'812': u'2015-07-09', u' 813': u'2015-07-11', u'348': u'2015-03-05', u'349': u'2015-04-0....
【问题讨论】:
-
什么是注册?
-
你能给我们看看你的样本数据吗?
-
@prashant 注册是类似于 non_udacity_enrollments 的字典。我将它复制到后者以删除一些值。回到我的电脑后,我会尝试发布代码。
-
@DennisKioko 你能展示你的样本数据吗?
标签: python python-2.7 dictionary