【问题标题】:How do I get data from dictionary in python?如何从 python 中的字典中获取数据?
【发布时间】:2022-01-24 07:52:03
【问题描述】:
a= int(input("Enter number of students:"))
i=1
n = []
t = []
p = []
avg = []

while i<=a:
    total=0
    name = input("Enter name of student:")
    n.append(name)
    subjects = int(input("Enter count of subjects:"))
    i=i+1
    j=1
    while j<=subjects:
        s = int(input("Enter marks:"))
        total = total + s
        j=j+1
        percentage = (total/(subjects*100))*100
        average = total/subjects
    t.append(total)
    p.append(percentage)
    avg.append(avg)
    
    
    result = dict(zip(n,p))
    
    
    print("Total marks of", name, "is", total)

print("The students:",n)
print("The total of students:",total)
print("The average of students:",avg)
print("The percentage of students:", percentage)
print("The result of students:", result)   

我想存储从这段代码中获得的数据,然后显示特定数据,例如如果我搜索学生的姓名,我会得到他的分数和平均分。我该怎么做?

【问题讨论】:

  • 最好能详细说明你遇到了什么问题,解决什么问题,预期的输出是什么。否则,用户应该花很多时间来理解您的问题。
  • 您的意思是将结果字典存储在文件中吗?
  • 如果您想稍后存储和使用字典,不妨看看这个:stackoverflow.com/questions/11218477/…

标签: python dictionary


【解决方案1】:

首先获取名称。然后找到那个索引。现在在数组中搜索标记、平均值等。请记住,这仅在每个学生数据的索引相同时才有效。想象一下,数组相互堆叠,每个学生的数据应该在一条直线上(您只在这里做过)​​。哦,还有一件事,它们被称为数组,而不是字典。字典用 {}(大括号)初始化。 这是一个示例代码:

n=["S1","S2"]
t=[100,70]
avg=[30,20]
#say you want to get the data of S2
i=n.index("S2")
print("Student name: "+str(n[i])+" Student total marks:"+str(t[i])+" Student average: "+str(avg[i]))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-23
    • 2021-12-24
    相关资源
    最近更新 更多