访问列表中多个词典的值



我正在为我的学校网站制作一个刮刀,它会自动获取今天的家庭作业。

[
{
"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=某事:发送AssignmentId以显示

我试着自己做,但我不知道如何解决这个问题,我如何阅读每一本字典,检查每一个关键字及其值,然后打印出同一本字典中的另一个关键字?

for key, value in diary:
if key == "date":
if value == "Fri, 04/11/2022":
print(key, value)

根据注释,您可以迭代列表并检查您的条件。例如(lst是您的问题列表(:

for d in lst:
if d["studentId"] == "8426" and d["date"] == "Fri, 04/11/2022":
print(d["assignmentId"])

打印:

35013
35007
35005
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'])

相关内容

  • 没有找到相关文章

最新更新