Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.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 如何反复浏览字典列表以确保I don';不加相同的名字吗?_Python_Json_List_Dictionary - Fatal编程技术网

Python 如何反复浏览字典列表以确保I don';不加相同的名字吗?

Python 如何反复浏览字典列表以确保I don';不加相同的名字吗?,python,json,list,dictionary,Python,Json,List,Dictionary,我有一个json文件,如下所示: [{"History": ["sally", "billy", "tommy"], "Calculus": [billy]}] 我希望遍历类的名称,以便在添加下一个类时,确保该类不在列表中。然后我想遍历这个类,以确保没有一个同名的学生被添加了两次 我制定了以下代码: with open('students.json') as j: data = j

我有一个json文件,如下所示:

[{"History": ["sally", "billy", "tommy"], "Calculus": [billy]}]
我希望遍历类的名称,以便在添加下一个类时,确保该类不在列表中。然后我想遍历这个类,以确保没有一个同名的学生被添加了两次

我制定了以下代码:

with open('students.json') as j:
       data = json.load(j)
for classRoom in data:
       print "entered here4"
       #classCheck = data[classRoom]
       #this checks if the classroom is already in the dictionary of lists
       if(className == classRoom):
          print "entered here5"        
          for student in data[classRoom]:
                if(studentName == student):
                print "student already in this classroom"
                       sys.exit()
          data[classRoom].append(studentName)
          with open ('students.json', 'w') as outfile:
                    json.dump(data,outfile)
           print "student added to class"
           sys.exit()

if(className==教室):
运行代码时跳过此if语句。如果我更改
classCheck==data[classk]
我会得到一个错误“列表索引必须是整数,而不是dict。我可以在for循环中更改什么,以便它检查教室名称是否存在?

正如注释中指出的,“class”是一个保留关键字,但除此之外,只有两个字典(“学生”和“班级”)检查学生或类是否已存在,然后选择添加该学生或类。

json是一个字典列表。当您迭代该列表时

for classRoom in data:
    # classRoom is the whole dictionary - {"History": ["sally", "billy", "tommy"], "Calculus": ["billy"]}
将JSON更改为如下所示的字典-

{"History": ["sally", "billy", "tommy"], "Calculus": ["billy"]}
现在,


以下是您应该做的: 假设
data=[{“History”:[“sally”,“billy”,“tommy”],“演算”:[billy]}]

for classRoom, studentList in data[0].items(): #this grabs both keys and values in the dictionary, then
    if className in classRoom:
        #do something ..
    for i in studentList: #since values are list
        if studentName in i:# this grabs each values list and check if student exist there
            #do somthing ...
          

“class”一词在面向对象编程语言中通常是一个保留字,因此请使用另一个变量名,如“classname”或“myclass”或“c”“@jarmod哦,对不起!我没有意识到。我更改了变量名。希望它能把事情弄清楚对不起,我改了。我还是个新手,python没有意识到。有没有办法改变我的for循环,让它可以检查键,而不是使用两个字典?因此,它可以进入if语句,查看是否添加了名称。感谢这有助于查找教室和名称,但当我想添加名称时,我在附加行中得到一个错误,即“列表索引必须是整数,而不是unicode”。如何更改附加名称,使其添加到该教室?编辑:算出了,谢谢!
for classRoom, studentList in data[0].items(): #this grabs both keys and values in the dictionary, then
    if className in classRoom:
        #do something ..
    for i in studentList: #since values are list
        if studentName in i:# this grabs each values list and check if student exist there
            #do somthing ...