-1
我正在讀取一些記錄,它們包含一個包含兩個更多字段的數組。例如,獲取的數據是這樣的:將數組值賦給本地變量
{"id": 1, "name": "Govind", "Array": [{"subject": "maths", "score": 70}, {"subject": "maths", "score": 80}, {"subject": "english", "score": 90}]}
我想的"score"
值賦給一個局部變量,其中"subject"
是"maths"
,讓我能有這樣的事情:
var1 = 70 and var2 = 80
代碼卡:
try :
cursor = db.students.find().sort('_id',pymongo.ASCENDING)
for loop in cursor:
for item in loop:
if loop[item] is "type":
#here i need to check value of array elements and then remove them from array.
print key #loop[item]
首先for循環給我記錄。 第二個循環遍歷其中一個是數組的項目 我還需要訪問數組元素並對較低的值進行比較。
輸出我得到的是這樣的
**193
govind
[{u'score': 47 , u'type': u'exam'}, {u'score': 41 ,
u'type': u'quiz'}, {u'score': 70 , u'type': u'homework'}, {u'score'
: 48 , u'type': u'homework'}]**
我想比較「數學」的得分,然後刪除與低分數組元素的數組和接入的元素上。 –
這個例子代表一個場景,而不是實際的數據。我的目的是從較低分數的陣列中刪除記錄。 –
我的最終輸出記錄是:{「id」:1,「name」:「Govind」,「Array」:[{「subject」:「maths」,「score」:80},{「subject」:「英語「,」分數「:90}] –