Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python列表中存储学生数据以获取百分比_Python_Python 3.x_List - Fatal编程技术网

在python列表中存储学生数据以获取百分比

在python列表中存储学生数据以获取百分比,python,python-3.x,list,Python,Python 3.x,List,输出:- import re import os import sys class Marks: def __init__(self): self.marks = [] self.marks_file = '/root/projectpython/mark.txt' def loadAll(self): file = open(self.marks_file, 'r') for line in file.readli

输出:-

import re
import os
import sys
class Marks:
    def __init__(self):
        self.marks = []
        self.marks_file = '/root/projectpython/mark.txt'
    def loadAll(self):
        file = open(self.marks_file, 'r')
        for line in file.readlines():
            name,math,phy,chem = line.strip().split()
            name=name
            math=int(math)
            phy=int(phy)
            chem=int(chem)
            self.marks=[name,math,phy,chem]
            print(self.marks)
        file.close()

    def percent(self):
        dash = '-' * 40
        self.loadAll()
        for n in self.marks:
            print(n)

Book_1 = Marks()
Book_1.percent()
但我想以表格格式打印所有值,它只显示最后一条记录。
使用列表存储学生数据名称和分数是否正确。

您继续在
for
语句中初始化列表 并声明它,以便只反映最后一行的数组值

我认为您可以删除初始化语句并将其作为
追加处理

['gk', 50, 40, 30]
['rahul', 34, 54, 30]
['rohit', 87, 45, 9]
rohit
87
45
9

这里的问题是读取的行

import re
import os
import sys
class Marks:
    def __init__(self):
        self.marks = []
        self.marks_file = '/root/projectpython/mark.txt'
    def loadAll(self):
        file = open(self.marks_file, 'r')
        for line in file.readlines():
            name,math,phy,chem = line.strip().split()
            name=name
            math=int(math)
            phy=int(phy)
            chem=int(chem)
            self.marks.append(name)
            self.marks.append(math)
            self.marks.append(phy)
            self.marks.append(chem)
            # self.marks=[name,math,phy,chem]
            print(self.marks)
        file.close()

    def percent(self):
        dash = '-' * 40
        self.loadAll()
        for n in self.marks:
            print(n)

Book_1 = Marks()
Book_1.percent()
这将在每次读取标记时继续重新初始化列表

而是使用:

self.marks=[name,math,phy,chem]

self.marks=[name,math,phy,chem]
设置为
self.marks.append([name,math,phy,chem])
。 然后最简单的解决方案是将
self.marks
列表转换并打印它们

假设你的
标记
列表是
[[gk',50,40,30],'rahul',34,54,30],'rohit',87,45,9]
,然后简单地转换它

self.marks.append([name,math,phy,chem])
输出:

print(marks)

transposed=list(zip(*marks))
print(transposed)
for x in transposed:
    print(x)
现在开始工作了。 我之前在这里犯的错误只有self.marks.append([name,math,phy,chem])


[['gk',50,40,30],'rahul',34,54,30],'rohit',87,45,9].

检查我的解决方案我已经为代码添加了扩展,因此输出看起来像一个表。
[['gk', 50, 40, 30], ['rahul', 34, 54, 30], ['rohit', 87, 45, 9]] #marks list

[('gk', 'rahul', 'rohit'), (50, 34, 87), (40, 54, 45), (30, 30, 9)] #transposed list


('gk', 'rahul', 'rohit')  # output the way you want
(50, 34, 87)
(40, 54, 45)
(30, 30, 9)