2016-04-23 8 views
1

Ich brauche Hilfe beim Sortieren meiner Schlüssel-Wert-Paar. Meine Ausgabe ist in dieser URL http://pastebin.com/ckKAtP5y. Aber was ich versucht habe zu tun ist.Python IndexError - Brauchen Sie Hilfe Sortierschlüssel und Wert

{ 
    "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 
    } 
] 

Also, was ich tun möchte, ist ich vergleichen wollen ‚CHEM‘ und ‚Chemie‘ in „COURSE_NAME“ und mich nur bekommen die meisten ‚course_mentioned_times‘ und die andere entfernen. In diesem Fall möchte ich CHEMISTRY 231, weil es 50 Mal erwähnt wird.

Hier ist, was mir bisher geholfen wurde.

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) 

Dieser Code gibt mir eine Indexerror data [ 'Kurse'] Pop (i) Indexerror:. Pop Indexbereich aus

+0

Nur für CHEM? Was ist mit Mathe? Möchtest du die ** max ** von natürlich genannten Zeiten für ähnliche Namen bekommen? Ich verstehe es nicht –

+0

Ja, ich möchte max of course_nowed_times für ähnliche Namen. Also auch für MATH. – Benji

+0

Wie planen Sie, sie zu vergleichen, wenn Sie Kurse wie ** B 103 ** haben. Ich sehe auch einen wie '' course_name ":" 7 ",'. –

Antwort

1

Nach viel hin und her in den Kommentaren Frage:

#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 "" 
+0

Du bist ein Lebensretter;) – Benji

+0

@Benji Ich weiß, es hat geholfen! Bitte stellen Sie dies als Antwort auf Ihre Frage ein, wenn dies der Fall ist. –

+0

Perez Albela Eine letzte Frage haha. Also versuche ich meine JSON-Datei zu sortieren und drucke einfach die Top 20 aus. Da wird Max auf Minimalwert gesetzt. Setzen Sie den oben genannten Max-Kurs oben auf meine JSON-Datei. Sortiere es so, dass es maximal ist. http://pastebin.com/S0R0tWUH -> Ich habe Ihren Code so geändert, dass ich die Ausgabe in meiner JSON-Datei speichern kann: http://pastebin.com/mqCACCP8 – Benji

1
import json 
import collections 

with open('output_info.json') as data_file: 
    data = json.load(data_file) 

courses = data['courses'] 

courses_by_prefix = collections.defaultdict(list) 

for course in courses: 
    prefix = course['course_name'].split(' ', 2)[0].upper()[:3] 
    courses_by_prefix[prefix].append(course) 


results = [] 

for prefix, courselist in courses_by_prefix.items(): 
    mosttimes = max(courselist, key=lambda c: c['course_mentioned_times']) 
    results.append(mosttimes) 

print(results)