2017-05-30 1 views
-1
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) 

出力に追加なっています。登録の多くの行が幅が広い['days_to_cancel']値を持っていても、すべての行がpaid_studentslistに追加されるのはなぜですか。これは私のために就学はなぜ、すべてのデータがリスト

{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

+0

コードで "> 7"をチェックしても問題ありませんか? –

+0

@AshKetchumはい「days_to_cancel」が7より大きい必要があるという質問で尋ねられました。 –

+3

*文字列*を整数と比較しています。 'u ''> 7'はPython 2では常に真です。なぜなら、数値は他のオブジェクト型の前に常に順序付けされているからです。値を整数* first *に変換します。 –

答えて

0

ため

例のデータを動作します:私は、ifの2番目のパラメータで)(int型を追加し、 あなたが刺され(あなたのDIC値)を比較しました。 Python 2ではコメントで指されるように常に真を返します。

enrollments= [{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'10', u'is_canceled': u'True', u'is_udacity': u'True', u'join_date': u'2015-01-27', u'status': u'canceled'}] 

paid_students=[] 
for students in enrollments: 
    if students['days_to_cancel']==None or int(students['days_to_cancel'])>7: 
     paid_students.append(students) 

print len(paid_students) 
+1

変更内容とその理由を説明してください。説明がなければ、これは有益な答えではありません。 –

+0

@MartijnPietersが追加されました –

+1

So * why *なぜあなたは 'int()'を追加しましたか?それ以外の場合は、なぜ、すべての要素を 'paid_students'に追加しますか? (はい、私はすでに答えは知っていますが、将来の訪問者はあなたの投稿からその情報を得ることができません)。 –

関連する問題