Python编程入门,请求讲解!急!

①随机生成30个学生的学号(不重复)和成绩( 0 ~100),并逐行写入“成绩.csv”文件中
②实现一个对嵌套列表指定列进行选择排序的函数,要求函数的参数为嵌套列表和指定列索引下标,函数没有返回值。
③从“成绩.csv”文件读出学生学号和成绩,用嵌套列表进行保存,再调用实现的排序函数,按照学生的成绩进行排序,并打印前5名的学号和成绩。

试一下

代码如下:

import random

"""
①随机生成30个学生的学号(不重复)和成绩( 0 ~100),并逐行写入“成绩.csv”文件中
②实现一个对嵌套列表指定列进行选择排序的函数,要求函数的参数为嵌套列表和指定列索引下标,函数没有返回值。
③从“成绩.csv”文件读出学生学号和成绩,用嵌套列表进行保存,再调用实现的排序函数,按照学生的成绩进行排序,并打印前5名的学号和成绩。
"""


def generate_func(input_file):
    _list = []
    _ids = []
    with open(input_file, "w") as fw:
        for i in range(30):
            _id = random.randint(1, 50)
            while _id in _ids:
                _id = random.randint(1, 50)
            score = random.randint(0, 100)
            temp = {}
            temp['id'] = _id
            temp['score'] = score
            _ids.append(_id)
            _list.append(temp)
            fw.write(str(_id) + '\t' + str(score) + '\n')
    return _list


def sort_func(_list, col):
    return sorted(_list, key=lambda k: k[col])


print("#####################################################################")
print("①随机生成30个学生的学号(不重复)和成绩( 0 ~100),并逐行写入“成绩.csv”文件中")
input_file = "成绩.csv"
output_file = "output.xlsx"
_list = generate_func(input_file)
print("成绩已成功录入\n", _list)
print("#####################################################################")
print("②实现一个对嵌套列表指定列进行选择排序的函数,要求函数的参数为嵌套列表和指定列索引下标,函数没有返回值。")
sort_list = sort_func(_list, 'score')
print("成绩排序已完成\n", sort_list)

print("#####################################################################")
print("③从“成绩.csv”文件读出学生学号和成绩,用嵌套列表进行保存,再调用实现的排序函数,按照学生的成绩进行排序,并打印前5名的学号和成绩。")
with open(input_file, "r") as fr:
    lines = fr.readlines()

_list = []
for line in lines:
    if line is not None and len(line) > 1:
        items = line.replace("\n", "").split("\t")
        _id = items[0]
        score = items[1]
        temp = {}
        temp['id'] = int(_id)
        temp['score'] = int(score)
        _list.append(temp)

result = sort_func(_list, 'score')
print(result)

输出如下:

#####################################################################
①随机生成30个学生的学号(不重复)和成绩( 0 ~100),并逐行写入“成绩.csv”文件中
成绩已成功录入
 [{'id': 20, 'score': 72}, {'id': 39, 'score': 78}, {'id': 26, 'score': 10}, {'id': 43, 'score': 83}, {'id': 5, 'score': 90}, {'id': 27, 'score': 99}, {'id': 10, 'score': 34}, {'id': 14, 'score': 85}, {'id': 50, 'score': 72}, {'id': 35, 'score': 87}, {'id': 30, 'score': 88}, {'id': 32, 'score': 44}, {'id': 15, 'score': 74}, {'id': 41, 'score': 34}, {'id': 17, 'score': 0}, {'id': 1, 'score': 18}, {'id': 11, 'score': 1}, {'id': 47, 'score': 5}, {'id': 2, 'score': 25}, {'id': 45, 'score': 42}, {'id': 8, 'score': 57}, {'id': 18, 'score': 48}, {'id': 46, 'score': 92}, {'id': 33, 'score': 51}, {'id': 3, 'score': 33}, {'id': 36, 'score': 67}, {'id': 44, 'score': 53}, {'id': 34, 'score': 63}, {'id': 24, 'score': 13}, {'id': 29, 'score': 44}]
#####################################################################
②实现一个对嵌套列表指定列进行选择排序的函数,要求函数的参数为嵌套列表和指定列索引下标,函数没有返回值。
成绩排序已完成
 [{'id': 17, 'score': 0}, {'id': 11, 'score': 1}, {'id': 47, 'score': 5}, {'id': 26, 'score': 10}, {'id': 24, 'score': 13}, {'id': 1, 'score': 18}, {'id': 2, 'score': 25}, {'id': 3, 'score': 33}, {'id': 10, 'score': 34}, {'id': 41, 'score': 34}, {'id': 45, 'score': 42}, {'id': 32, 'score': 44}, {'id': 29, 'score': 44}, {'id': 18, 'score': 48}, {'id': 33, 'score': 51}, {'id': 44, 'score': 53}, {'id': 8, 'score': 57}, {'id': 34, 'score': 63}, {'id': 36, 'score': 67}, {'id': 20, 'score': 72}, {'id': 50, 'score': 72}, {'id': 15, 'score': 74}, {'id': 39, 'score': 78}, {'id': 43, 'score': 83}, {'id': 14, 'score': 85}, {'id': 35, 'score': 87}, {'id': 30, 'score': 88}, {'id': 5, 'score': 90}, {'id': 46, 'score': 92}, {'id': 27, 'score': 99}]
#####################################################################
③从“成绩.csv”文件读出学生学号和成绩,用嵌套列表进行保存,再调用实现的排序函数,按照学生的成绩进行排序,并打印前5名的学号和成绩。
[{'id': 17, 'score': 0}, {'id': 11, 'score': 1}, {'id': 47, 'score': 5}, {'id': 26, 'score': 10}, {'id': 24, 'score': 13}, {'id': 1, 'score': 18}, {'id': 2, 'score': 25}, {'id': 3, 'score': 33}, {'id': 10, 'score': 34}, {'id': 41, 'score': 34}, {'id': 45, 'score': 42}, {'id': 32, 'score': 44}, {'id': 29, 'score': 44}, {'id': 18, 'score': 48}, {'id': 33, 'score': 51}, {'id': 44, 'score': 53}, {'id': 8, 'score': 57}, {'id': 34, 'score': 63}, {'id': 36, 'score': 67}, {'id': 20, 'score': 72}, {'id': 50, 'score': 72}, {'id': 15, 'score': 74}, {'id': 39, 'score': 78}, {'id': 43, 'score': 83}, {'id': 14, 'score': 85}, {'id': 35, 'score': 87}, {'id': 30, 'score': 88}, {'id': 5, 'score': 90}, {'id': 46, 'score': 92}, {'id': 27, 'score': 99}]

Process finished with exit code 0


输出的01.csv

2072
3978
2610
4383
590
2799
1034
1485
5072
3587
3088
3244
1574
4134
170
118
111
475
225
4542
857
1848
4692
3351
333
3667
4453
3463
2413
2944

如有问题及时沟通

学号的格式是什么样子的?

  1. csv文件格式要求是什么?直接是 学号,成绩,每行1个?
  2. 嵌套列表的格式要求是什么?[[学号1,成绩1],[学号2,成绩2]...]?
  3. 学号是否统一为2位数字?是否允许1位?如果是2位,是否是10-99范围内?

写成绩.csv 部分

import  random
IDList=[x  for  x in range(10,100)]
# 下面是洗牌算法实现随机学号生成,后面取前30个
for i in range(90):
    t=random.randint(0,89)
    tID=IDList[i]
    IDList[i]=IDList[t]
    IDList[t]=tID

# 写到csv文件,假定每行 是  学号,成绩 这样格式 
csvfile=open("成绩.csv","w")
csvDatas=[]
for i in range(30):
    tcj = random.randint(0,100)
    csvDatas.append("{},{}".format(IDList[i],tcj))
csvfile.write("\n".join(csvDatas))
csvfile.close()

读取CSV文件,处理部分:

def mySort(inList, idx ):
    inList.sort(key=lambda x:x[idx],reverse=True)
csvfile=open("成绩.csv","r")

# 生成嵌套列表
DataList=[]
for line in csvfile.readlines():
    line=line.strip()
    if (line !=""):
        atmp=line.split(",")
        DataList.append([int(atmp[0]),int(atmp[1])])
# 排序
mySort(DataList,1)
# 打印前5for  i in range(5):
    print(DataList[i])