gpt4 book ai didi

Python IndexError - 需要帮助排序键和值

转载 作者:行者123 更新时间:2023-12-01 03:59:38 28 4
gpt4 key购买 nike

我需要帮助对我的键值对进行排序。我的输出位于此网址 http://pastebin.com/ckKAtP5y 中。然而,我一直在努力做的是。

{
"courses": [
{
"professors": [
{
"first_name": "Zvezdelina",
"last_name": "Stankova",
"professor_url": "http://www.ratemyprofessors.com/ShowRatings.jsp?tid=375269",
"helpfullness": 4.3,
"clarity": 4.3,
"overall_rating": 4.3
}],
"course_name": "CHEM 1",
"course_mentioned_times": 37
},
{
"professors": [
{
"first_name": "Alan",
"last_name": "Shabel",
"professor_url": "http://www.ratemyprofessors.com/ShowRatings.jsp?tid=1309831",
"helpfullness": 3.9,
"clarity": 3.5,
"overall_rating": 3.7
}],
"course_name": "CHEMISTRY 231",
"course_mentioned_times": 50
}
]

所以我想做的是比较“course_name”中的“CHEM”和“CHEMISTRY”,然后获取最多的“course_thought_times”并删除另一个。在本例中,我想要 CHEMISTRY 231,因为它被提及了 50 次。

这是迄今为止我得到的帮助。

if __name__ == "__main__":
import json

#'output_info.json is http://pastebin.com/ckKAtP5y
with open('output_info.json') as data_file:
data = json.load(data_file)

temp_data = data
greater = []
len1 = len(data['courses'])
len2 = len1

for i in range(0,len1):
for j in range(0, len2):
if i==j:
continue
if data['courses'][i]['course_name'][0] == temp_data['courses'][j]['course_name'][0]:
if data['courses'][i]['course_name'][1] == temp_data['courses'][j]['course_name'][1]:
if data['courses'][i]['course_name'][2] == temp_data['courses'][j]['course_name'][2]:
if data['courses'][i]['course_mentioned_times']> temp_data['courses'][j]['course_mentioned_times']:
greater.append(i)
else:
greater.append(j)


final = []
for i in greater:
if i not in final:
final.append(i)

list_order = []

for i in range(0,len(data['courses'])):
list_order.append(i)

new_final = []
for i in list_order:
if i not in final:
new_final.append(i)

for i in new_final:
if i!=new_final[0]:
i=i-1
data['courses'].pop(i)

# Writing the new json data back to data.json file.
with open('data.json', 'w') as f:
json.dump(data, f)

这段代码给了我一个 IndexError 数据['类(class)'].pop(i)IndexError:弹出索引超出范围

最佳答案

经过多次反复讨论问题评论后:

#coding:utf-8

import json

filename = 'data.json'

with open(filename, 'r') as f:
data = json.load(f)
courses = data.get('courses', None)

if courses:
keys = sorted(set([course.get('course_name', None).strip().split()[0][0:3] for course in courses]))

results = {'courses': {}}

for key in keys:
results['courses'][key] = []
temp = {}
for course in courses:
course_name = course.get('course_name', None)
professors = course.get('professors', None)
if course_name.strip().split()[0][0:3] == key:
course_mentioned_times = course.get('course_mentioned_times')
temp[course_name] = {'course_mentioned_times':course_mentioned_times, 'professors': professors}
results['courses'][key] = temp
else:
raise Exception('No courses could be found on {}'.format(filename))

def get_most_mentioned(name):
name = name[0:3]
data = results.get('courses', None).get(name)
max_mentioned_times = max(map(lambda m: data.get(m, None).get('course_mentioned_times'), data.keys()))

most_mentioned = []
for course_name, values in data.items():
course_mentioned_times = values.get('course_mentioned_times', None)
if course_mentioned_times == max_mentioned_times:
most_mentioned.append({'course_name': course_name, 'course_mentioned_times': course_mentioned_times, \
'professors': values.get('professors')})
return most_mentioned

print "Course with most mentioned times:"
print "---------------------------------"
for key in keys:
print "[*] For Key '{}':".format(key)
for item in get_most_mentioned(key):
course_name = item.get('course_name', None)
print " Course Name: {}".format(course_name)
print " Mentioned Times: {}\n".format(item.get('course_mentioned_times'))
print " Professors:\n"
for i, professor in enumerate(item.get('professors', None), start=1):
print " {}) Full name: {} {}".format(i, professor.get('first_name'), professor.get('last_name'))
print " URL: {}".format(professor.get('professor_url'))
print " Helpfullness: {}".format(professor.get('helpfullness'))
print " Clarity: {}".format(professor.get('clarity'))
print " Overall_rating: {}".format(professor.get('overall_rating'))
print ""
print ""

关于Python IndexError - 需要帮助排序键和值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36817176/

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