json文件的数据格式确定:
版本V1:
{ "张三":{ "English": 30.0, "Music": 45.56, "Chinese": 100.9 } }
版本V2:
{ "Name": "张三", "English": 30.0, "Music": 45.56, "Chinese": 100.9 }
版本V3:
{ "Name": "张三", "Mark": { "English": 30.0, "Music": 45.56, "Chinese": 100.9 } }
版本V4:
{ "Name":"张三", "Gender":"male", "Age":16, "Mark":[ { "Subject":"English", "Score": 30.0 }, { "Subject":"Music", "Score": 30.0 }, { "Subject":"Chinese", "Score": 30.0 } ] }
版本V4错误格式:
{ "Name": "张三", "Gender":"male", "Age":16, "Mark":{ { "Subject":"English", "Score": 30.0, }, { "Subject":"Music", "Score": 30.0 , }, { "Subject":"Chinese", "Score": 30.0, } } }
版本V5: stu_mark5.json
[{ "Name": "张三", "Gender":"male", "Age":16, "Mark":[ { "Subject":"English", "Score": 60.0 }, { "Subject":"Music", "Score": 80.0 }, { "Subject":"Chinese", "Score": 30.0 } ] }, { "Name": "里斯", "Gender":"male", "Age":15, "Mark":[ { "Subject":"English", "Score": 90.0 }, { "Subject":"Music", "Score": 90.0 }, { "Subject":"Chinese", "Score": 37.0 } ] }, { "Name": "王莎莎", "Gender":"female", "Age":18, "Mark":[ { "Subject":"English", "Score": 100.0 }, { "Subject":"Music", "Score": 100.0 }, { "Subject":"Chinese", "Score": 10.0 } ] } ]
以上版本有一个格式是错误的,其他版本格式都是正确的,都有自身的优点及缺点。
Python 核心代码:
参考1:
stuDict={"zs":100,"wang":86,"liu":67} sName=input("请输入学生的名称:") if(sName not in stuDict): print("你输入的学生不在.") else: print("此学生分数为:",stuDict[sName]) #print(stuDict.get(sName,"你输入的学生不在."))
参考2:
import json with open("stu_mark3.json", mode='r', encoding='utf-8') as fp: stu_mark=json.load(fp) print(stu_mark) print(type(stu_mark))
参考3:
import json stu_mark={} stu_mark["Name"]=input("Name=") stu_mark["English"]=float(input("English=")) stu_mark["Music"]=float(input("Music=")) stu_mark["Chinese"]=float(input("Chinese=")) print(stu_mark) with open("stu_mark.json", mode='w', encoding='utf-8') as fp: json.dump(stu_mark, fp,ensure_ascii=False,indent=2)
参考4:
import json stuList=list() with open("stu_mark4.json", mode='r', encoding='utf-8') as fp: stuList=json.load(fp) # print(stuList) for e in stuList: print(e["Name"],e["Gender"],e["Age"]) for s in e["Mark"]: print(s["Subject"],s["Score"]) print("*"*50)
几个目标: 求最高分,求最低分,求平均分,求排序,求名次,求柱状图。
参考代码1:
import json def printStuList(stuList): for e in stuList: sf=0 print(e["Name"],e["Gender"],e["Age"],end=' ') for s in e["Mark"]: print(s["Subject"],s["Score"],end=' ') sf+=s["Score"] print("Average",sf/3.0,end=' ') print("") print("*"*50) stuList=list() with open("stu_mark5.json", mode='r', encoding='utf-8') as fp: stuList=json.load(fp) printStuList(stuList) print("\n\n按照年龄排序后:\n") stuList.sort(key=lambda item:item["Age"]) printStuList(stuList) print("\n\n按照英语成绩排序后:\n") stuList.sort(key=lambda item:item["Mark"][0]["Score"]) printStuList(stuList) print("\n\n按照平均分排序后:\n") stuList.sort(key=lambda item:(item["Mark"][0]["Score"]+item["Mark"][1]["Score"]+item["Mark"][2]["Score"])/3) printStuList(stuList)
参考代码2:
import json def calSum(eJson): return eJson["Score"] def printStuList(stuList): for e in stuList: sf=0 print(e["Name"],e["Gender"],e["Age"],end=' ') for s in e["Mark"]: print(s["Subject"],s["Score"],end=' ') sf=sum(list(map(calSum,e["Mark"]))) print("Sum",sf,end=' ') e["Sum"]=sf print("") print("*"*50) stuList=list() with open("stu_mark4.json", mode='r', encoding='utf-8') as fp: stuList=json.load(fp) printStuList(stuList) print(stuList) print("\n\n按照年龄排序后:\n") stuList.sort(key=lambda item:item["Age"]) printStuList(stuList) print("\n\n按照英语成绩排序后:\n") stuList.sort(key=lambda item:item["Mark"][0]["Score"]) printStuList(stuList) print("\n\n按照Sum总分排序后:\n") stuList.sort(key=lambda item:(item["Sum"])) printStuList(stuList)
代码3:
import json import matplotlib.pyplot as plt def calSum(eJson): return eJson["Score"] def printStuList(stuList): for e in stuList: sf=0 print(e["Name"],e["Gender"],e["Age"],end=' ') for s in e["Mark"]: print(s["Subject"],s["Score"],end=' ') sf=sum(list(map(calSum,e["Mark"]))) print("Sum",sf,end=' ') e["Sum"]=sf print("") print("*"*50) stuList=list() with open("stu_mark4.json", mode='r', encoding='utf-8') as fp: stuList=json.load(fp) printStuList(stuList) print(stuList) print("\n\n按照年龄排序后:\n") stuList.sort(key=lambda item:item["Age"]) printStuList(stuList) print("\n\n按照英语成绩排序后:\n") stuList.sort(key=lambda item:item["Mark"][0]["Score"]) printStuList(stuList) print("\n\n按照Sum总分排序后:\n") stuList.sort(key=lambda item:(item["Sum"])) printStuList(stuList) nameList=list() sumList=list() for e in stuList: nameList.append(e["Name"]) sumList.append(e["Sum"]) # nameTuple=tuple(nameList) nameTuple=tuple() nameTuple=tuple(nameList) plt.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams['axes.unicode_minus'] = False plt.bar(nameTuple, sumList) plt.title('总分柱状图') plt.show()
代码4:
import json import matplotlib.pyplot as plt import numpy as np def calSum(eJson): return eJson["Score"] def printStuList(stuList): for e in stuList: sf=0 print(e["Name"],e["Gender"],e["Age"],end=' ') for s in e["Mark"]: print(s["Subject"],s["Score"],end=' ') sf=sum(list(map(calSum,e["Mark"]))) print("Sum",sf,end=' ') e["Sum"]=sf print("") print("*"*50) stuList=list() with open("stu_mark4.json", mode='r', encoding='utf-8') as fp: stuList=json.load(fp) printStuList(stuList) print(stuList) print("\n\n按照年龄排序后:\n") stuList.sort(key=lambda item:item["Age"]) printStuList(stuList) print("\n\n按照英语成绩排序后:\n") stuList.sort(key=lambda item:item["Mark"][0]["Score"]) printStuList(stuList) print("\n\n按照Sum总分排序后:\n") stuList.sort(key=lambda item:(item["Sum"])) printStuList(stuList) nameList=list() sub1List=list() sub2List=list() sub3List=list() for e in stuList: nameList.append(e["Name"]) sub1List.append(e["Mark"][0]["Score"]) sub2List.append(e["Mark"][1]["Score"]) sub3List.append(e["Mark"][2]["Score"]) # nameTuple=tuple(nameList) nameTuple=tuple() nameTuple=tuple(nameList) plt.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams['axes.unicode_minus'] = False bar_width=0.3 index0=np.arange(len(sub1List)) index1=index0+bar_width index2=index1+bar_width plt.bar(index0,height=sub1List,width=bar_width,color='b',label="English") plt.bar(index1,height=sub2List,width=bar_width,color='g',label="Music") plt.bar(index2,height=sub3List,width=bar_width,color='r',label="Chinese") plt.legend() # 显示图例 plt.xticks(index0 + bar_width/2, nameList) # 让横坐标轴刻度显示 waters 里的饮用水, index_male + bar_width/2 为横坐标轴刻度的位置 plt.ylabel('成绩') # 纵坐标轴标题 plt.title('成绩柱状图') plt.show()
import json import matplotlib.pyplot as plt import numpy as np def printStuList(stuList): for e in stuList: sf=0 print(e["Name"],e["Gender"],e["Age"],end=' ') for s in e["Mark"]: print(s["Subject"],s["Score"],end=' ') sf=sum(list(map(calSum,e["Mark"]))) print("Sum",sf,end=' ') e["Sum"]=sf print("") print("*"*50) stuList=list() with open("stu_mark4.json", mode='r', encoding='utf-8') as fp: stuList=json.load(fp) print(stuList) # nameList=list() # sub1List=list() # sub2List=list() # sub3List=list() nameList=[e["Name"] for e in stuList] print(nameList) sub1List=[e["Mark"][0]["Score"] for e in stuList] sub2List=[e["Mark"][1]["Score"] for e in stuList] sub3List=[e["Mark"][2]["Score"] for e in stuList] print(sub1List,sub2List,sub3List) plt.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams['axes.unicode_minus'] = False bar_width=0.3 index0=np.arange(len(sub1List)) index1=index0+bar_width index2=index1+bar_width plt.bar(index0,height=sub1List,width=bar_width,color='b',label="English") plt.bar(index1,height=sub2List,width=bar_width,color='g',label="Music") plt.bar(index2,height=sub3List,width=bar_width,color='r',label="Chinese") plt.legend() # 显示图例 plt.xticks(index0 + bar_width/2, nameList) # 让横坐标轴刻度显示 nameList 里的饮用水, index_male + bar_width/2 为横坐标轴刻度的位置 plt.ylabel('成绩') # 纵坐标轴标题 plt.title('成绩柱状图') plt.show()
基础训练:
m1.py
import numpy as np import matplotlib.pyplot as plt #1 # x=[3,4,5,6,7] # y=[30,45,50,60,70] # plt.bar(x,y) # plt.plot(x,y,c=u'y') # plt.scatter(x,y,c=u'r',marker=u'+') # plt.show() # #2 # x=np.array([3,4,5,6,7]) # y=2*x+10 # plt.bar(x,y) # plt.plot(x,y,c=u'y') # plt.scatter(x,y,c=u'r',marker=u'+') # plt.show() #3 # x=np.array([0,3,4,5,6]) # y=2*x**2+10*x+3 # plt.bar(x,y) # plt.plot(x,y,c=u'y') # plt.scatter(x,y,c=u'r',marker=u'+') # plt.show() #4 # x=np.arange(0,100,0.5) # y=2*x**2+10*x+3 # plt.bar(x,y) # plt.plot(x,y,c=u'y') # plt.scatter(x,y,c=u'r',marker=u'+') # plt.show() #5 x=np.linspace(0,100,200) y=2*x**2+10*x+3 plt.bar(x,y) plt.plot(x,y,c=u'y') plt.scatter(x,y,c=u'r',marker=u'+') plt.show()
m2.py
import numpy as np import matplotlib.pyplot as plt plt.figure(1) ax1=plt.subplot(3,2,1) ax2=plt.subplot(3,2,2) ax3=plt.subplot(3,2,3) ax4=plt.subplot(3,2,4) ax5=plt.subplot(3,2,5) #1 x=[3,4,5,6,7] y=[30,45,50,60,70] plt.sca(ax1) plt.plot(x,y,c=u'y') plt.scatter(x,y,c=u'r',marker=u'+') # #2 x2=np.array([3,4,5,6,7]) y2=2*x2+10 plt.sca(ax2) plt.bar(x2,y2) plt.plot(x2,y2,c=u'y') plt.scatter(x2,y2,c=u'r',marker=u'+') #3 x3=np.array([0,3,4,5,6]) y3=2*x3**2+10*x3+3 plt.sca(ax3) plt.bar(x3,y3) plt.plot(x3,y3,c=u'y') plt.scatter(x3,y3,c=u'r',marker=u'+') #4 x4=np.arange(0,100,0.5) y4=2*x4**2+10*x4+3 plt.sca(ax4) plt.bar(x4,y4) plt.plot(x4,y4,c=u'y') plt.scatter(x4,y4,c=u'r',marker=u'+') #5 x5=np.linspace(0,100,200) y5=2*x5**2+10*x5+3 plt.sca(ax5) plt.bar(x5,y5) plt.plot(x5,y5,c=u'y') plt.scatter(x5,y5,c=u'r',marker=u'+') plt.show()
----------------
算法代码解析:
def GetList(iMax): intList=[] for i in range(1,iMax+1): if(i%7==0): intList.append(i) return intList