例如,如果我有一个列表:
courses = [{name: a, course: math, count:1}]
如果我再次输入 name: a course: math 列表将是
courses = {name: a, course: math, count:2}
我只希望具有相同名称的项目和课程不会附加到列表中,而只会增加“计数”关键项目。
我试过 :
def add_class(inputname,inputcourse):
for i in (len(courses)):
if courses[i]['name']== inputname and courses[i]['course']==inputcourse:
courses[i][count]+=1
else :
newdata = {"name":inputname, "course":inputcourse,count:1}
#i put count because this item is the first time.
courses.append(newdata)
print courses
我希望输出是 class ={name: a, course: math, count:2}但实际输出是 class =[{name: a, course: math, count:2},{name: a, course: math, count:1}] 如果我输入一个新数据,如 name : a, course: physic 输出将是 [{name:a,course:physic,count:1},{name: a, course: math, count:2},{name: a, course: math, count:1}]
沧海一幻觉
相关分类