我正在為我的學校網站制作一個刮板,它會自動獲取今天的作業。
[
{
"id": "1523958",
"studentId": "8326",
"assignmentId": "35074",
"diaryType": "cw",
"diaryId": "35074",
"subject": "URDU",
"title": "???? ???? ? ?????",
"description": "9F Maymaar - Classwork posted",
"bRead": "1",
"date": "Fri, 04/11/2022",
},
{
"id": "1520938",
"studentId": "8426",
"assignmentId": "35013",
"diaryType": "cw",
"diaryId": "35013",
"subject": "SOCIAL STUDIES",
"title": "The Globe and Maps ",
"description": "5C Maymaar - Classwork posted",
"bRead": "0",
"date": "Fri, 04/11/2022",
"download": "2bf0c7b51af0cfb1023261dd7b8b4f8f.jpg",
},
{
"id": "1520624",
"studentId": "8426",
"assignmentId": "35007",
"diaryType": "cw",
"diaryId": "35007",
"subject": "MATHEMATICS",
"title": "Percentage ",
"description": "5C Maymaar - Classwork posted",
"bRead": "0",
"date": "Fri, 04/11/2022",
},
{
"id": "1520530",
"studentId": "8426",
"assignmentId": "35005",
"diaryType": "cw",
"diaryId": "35005",
"subject": "ENGLISH A",
"title": "Paragraph writing ",
"description": "5C Maymaar - Classwork posted",
"bRead": "0",
"date": "Fri, 04/11/2022",
},
{
"id": "1517928",
"studentId": "8326",
"assignmentId": "34952",
"diaryType": "cw",
"diaryId": "34952",
"subject": "PHYSICS",
"title": "Homework",
"description": "9F Maymaar - Classwork posted",
"bRead": "1",
"date": "Fri, 04/11/2022",
},
{
"id": "1513747",
"studentId": "8426",
"assignmentId": "34887",
"diaryType": "gn",
"diaryId": "34887",
"subject": None,
"title": "General notice ",
"description": "5C Maymaar - Notice posted",
"bRead": "0",
"date": "Thu, 03/11/2022",
},
{
"id": "1508998",
"studentId": "8426",
"assignmentId": "34787",
"diaryType": "cw",
"diaryId": "34787",
"subject": "SOCIAL STUDIES",
"title": "Map reading skills ",
"description": "5C Maymaar - Classwork posted",
"bRead": "0",
"date": "Thu, 03/11/2022",
},
]
你在上面看到的是一個串列,我想要做的是:如果 studentId = 8426 并且如果 date = something:發送 AssignmentiId 到顯示
我試著自己做,但我不知道如何解決這個問題,我如何閱讀每個字典,檢查每個鍵及其值,然后在同一個字典中列印出另一個鍵?
for key, value in diary:
if key == "date":
if value == "Fri, 04/11/2022":
print(key, value)
uj5u.com熱心網友回復:
根據評論,您可以遍歷串列并檢查您的情況。例如(lst是您的問題串列):
for d in lst:
if d["studentId"] == "8426" and d["date"] == "Fri, 04/11/2022":
print(d["assignmentId"])
印刷:
35013
35007
35005
uj5u.com熱心網友回復:
diaries = [
{
"id": "1523958",
"studentId": "8326",
"assignmentId": "35074",
"diaryType": "cw",
"diaryId": "35074",
"subject": "URDU",
"title": "???? ???? ? ?????",
"description": "9F Maymaar - Classwork posted",
"bRead": "1",
"date": "Fri, 04/11/2022",
},
{
"id": "1520938",
"studentId": "8426",
"assignmentId": "35013",
"diaryType": "cw",
"diaryId": "35013",
"subject": "SOCIAL STUDIES",
"title": "The Globe and Maps ",
"description": "5C Maymaar - Classwork posted",
"bRead": "0",
"date": "Fri, 04/11/2022",
"download": "2bf0c7b51af0cfb1023261dd7b8b4f8f.jpg",
},
{
"id": "1520624",
"studentId": "8426",
"assignmentId": "35007",
"diaryType": "cw",
"diaryId": "35007",
"subject": "MATHEMATICS",
"title": "Percentage ",
"description": "5C Maymaar - Classwork posted",
"bRead": "0",
"date": "Fri, 04/11/2022",
},
{
"id": "1520530",
"studentId": "8426",
"assignmentId": "35005",
"diaryType": "cw",
"diaryId": "35005",
"subject": "ENGLISH A",
"title": "Paragraph writing ",
"description": "5C Maymaar - Classwork posted",
"bRead": "0",
"date": "Fri, 04/11/2022",
},
{
"id": "1517928",
"studentId": "8326",
"assignmentId": "34952",
"diaryType": "cw",
"diaryId": "34952",
"subject": "PHYSICS",
"title": "Homework",
"description": "9F Maymaar - Classwork posted",
"bRead": "1",
"date": "Fri, 04/11/2022",
},
{
"id": "1513747",
"studentId": "8426",
"assignmentId": "34887",
"diaryType": "gn",
"diaryId": "34887",
"subject": None,
"title": "General notice ",
"description": "5C Maymaar - Notice posted",
"bRead": "0",
"date": "Thu, 03/11/2022",
},
{
"id": "1508998",
"studentId": "8426",
"assignmentId": "34787",
"diaryType": "cw",
"diaryId": "34787",
"subject": "SOCIAL STUDIES",
"title": "Map reading skills ",
"description": "5C Maymaar - Classwork posted",
"bRead": "0",
"date": "Thu, 03/11/2022",
},
]
for diary in diaries:
if diary['studentId'] == '8326' and diary['date'] == 'something':
print(diary['assignmentId'])
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/528188.html
標籤:Python列表字典
上一篇:字典值的平均值
