paid_students=[]
for students in enrollments:
if students['days_to_cancel']==None or students['days_to_cancel']>7:
paid_students.append(students)
print len(paid_students)
1640
的len(enrollments)
的价值也1640
。为什么所有行都会附加到paid_students
list
后面,即使注册中有很多行具有宽范围的['days_to_cancel']
值。
的入学
实例数据{u'account_key': u'448',
u'cancel_date': u'2014-11-10',
u'days_to_cancel': u'5',
u'is_canceled': u'True',
u'is_udacity': u'True',
u'join_date': u'2014-11-05',
u'status': u'canceled'}
{u'account_key': u'448',
u'cancel_date': u'2015-01-27',
u'days_to_cancel': u'0',
u'is_canceled': u'True',
u'is_udacity': u'True',
u'join_date': u'2015-01-27',
u'status': u'canceled'}
来源,Udacity
检查代码中的“> 7”是否正确? –
@AshKetchum是在问题中询问'days_to_cancel'必须大于7. –
您正将* strings *与一个整数进行比较。在Python 2中,'u''> 7'总是如此,因为数字总是在其他对象类型之前排序。将该值转换为一个整数* first *。 –