gpt4 book ai didi

python - 根据 JSON 文件中的特定参数制作元素列表

转载 作者:太空宇宙 更新时间:2023-11-03 11:57:51 25 4
gpt4 key购买 nike

我正在为学校做作业,我被要求列出特定年份和类别的诺贝尔和平奖获得者名单。这是 JSON 文件的示例:

[{
'year': '2018',
'category': 'physics',
'overallMotivation': '“for groundbreaking inventions in the field of laser physics”',
'laureates': [{
'id': '960',
'firstname': 'Arthur',
'surname': 'Ashkin',
'motivation': '"for the optical tweezers and their application to biological systems"',
'share': '2'
}, {
'id': '961',
'firstname': 'Gérard',
'surname': 'Mourou',
'motivation': '"for their method of generating high-intensity, ultra-short optical pulses"',
'share': '4'
}, {
'id': '962',
'firstname': 'Donna',
'surname': 'Strickland',
'motivation': '"for their method of generating high-intensity, ultra-short optical pulses"',
'share': '4'
}
]
}, {
'year': '2018',
'category': 'chemistry',
'laureates': [{
'id': '963',
'firstname': 'Frances H.',
'surname': 'Arnold',
'motivation': '"for the directed evolution of enzymes"',
'share': '2'
}, {
'id': '964',
'firstname': 'George P.',
'surname': 'Smith',
'motivation': '"for the phage display of peptides and antibodies"',
'share': '4'
}, {
'id': '965',
'firstname': 'Sir Gregory P.',
'surname': 'Winter',
'motivation': '"for the phage display of peptides and antibodies"',
'share': '4'
}
]
}
]

我应该找到一种方法来查找特定类别和年份的获胜者的全名,这是我当前的代码

def get_laureates(dict_prizes, year = "none", category = "none"):


names = []
for row in dict_prizes:
if row["category"] == category:
names.append(row["firstname"] + row['surname'])
return names

year = 2018
category = "peace"

get_laureates(dict_prizes, year = 2018, category = "peace")

这是输出

    TypeError                                 Traceback (most recent call last)
<ipython-input-168-57a2293c1bca> in <module>
11 category = "peace"
12 # test your function here
---> 13 get_laureates(dict_prizes, category = "peace")

<ipython-input-168-57a2293c1bca> in get_laureates(dict_prizes, year, category)
4 names = []
5 for row in dict_prizes:
----> 6 if row["category"] == category and row["year"] == year:
7 names.append(row["firstname"] + row['surname'])
8 return names

TypeError: string indices must be integers

我知道这段代码中有很多错误,我无法将年份转换为整数,即使删除了“年”参数,我也无法生成仅包含类别的结果。任何帮助将不胜感激,因为我对 JSON 完全一无所知(给我的阅读 Material 实际上只是教会了我转储和加载)。

最佳答案

您的代码通常可以正常工作,这意味着您的输入数据中缺少您未在此处发布的内容。我无法用您提供给我们的数据重现您看到的确切错误。

有一个问题您没有考虑——您还需要遍历 laureates,然后找出获胜者,因为那也是一个字典列表。我在下面做了:

dict_prizes = [{'year': '2018', 'category': 'physics', 'overallMotivation': '“for       groundbreaking inventions in the field of laser physics”', 'laureates': [{'id': '960', 'firstname': 'Arthur', 'surname': 'Ashkin', 'motivation': '"for the optical tweezers and their application to biological systems"', 'share': '2'}, {'id': '961', 'firstname': 'Gérard', 'surname': 'Mourou', 'motivation': '"for their method of generating high-intensity, ultra-short optical pulses"', 'share': '4'}, {'id': '962', 'firstname': 'Donna', 'surname': 'Strickland', 'motivation': '"for their method of generating high-intensity, ultra-short optical pulses"', 'share': '4'}]}, {'year': '2018', 'category': 'chemistry', 'laureates': [{'id': '963', 'firstname': 'Frances H.', 'surname': 'Arnold', 'motivation': '"for the directed evolution of enzymes"', 'share': '2'}, {'id': '964', 'firstname': 'George P.', 'surname': 'Smith', 'motivation': '"for the phage display of peptides and antibodies"', 'share': '4'}, {'id': '965', 'firstname': 'Sir Gregory P.', 'surname': 'Winter', 'motivation': '"for the phage display of peptides and antibodies"', 'share': '4'}]}]

def get_laureates(dict_prizes, year = "none", category = "none"):
names = []
for row in dict_prizes:
if row["category"] == category:
for winner in row['laureates']: # You need this loop for the inner list of dictionaries
names.append(winner['firstname'] + winner['surname'])
return names

year = 2018
category = "physics"

get_laureates(dict_prizes=dict_prizes, year=2018, category=category)

得到:

>>> get_laureates(dict_prizes=dict_prizes, year=2018, category=category)
['ArthurAshkin', 'GérardMourou', 'DonnaStrickland']

因为你在 year 上课,所以你也需要解决那部分问题,因为你没有,所以我没有包括在内。

关于python - 根据 JSON 文件中的特定参数制作元素列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58192302/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com