在以下示例中,我将数据包含在字典中。应用一些代码后,正确的输出应为值 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'状态':你已经取消了',你很可能':你'真的',你' is_canceled':你& #39; True',你' join_date':u' 2014-11-10',u' account_key':u''',你&# 39; cancel_date&#39 ;: u' 2015-01-14',u' days_to_cancel':u' 65'}
输出打印paid_students
{u' 199':u' 2015-05-30',u'':u' 2015-04-02',u&# 39; 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'':u' 2015-05-12',u' 1257':u' 2015-07-09&#39 ;,' 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& #39; 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',你'你' 2014-11-10',你'':你' 2014-11-10',你' 812&# 39 ;: u' 2015-07-09',u'':u' 2015-07-11',u' 348':u&#39 ; 2015-03-05',u' 349':u' 2015-04-0 ....
答案 0 :(得分:1)
is_canceled
的值是字符串,而不是布尔值,因此您可以执行not enrollment['is_canceled']
,因为这将始终为真。
与days_to_cancel
的值相同 - 您将unicode值与整数进行比较,这也会给您带来奇怪的结果。
所以条件应该是这样的:
if (enrollment['is_canceled'] == 'False' or int(enrollment['days_to_cancel']) > 7):