我有2个JSON文件。一份学生档案和一份学生档案。你知道吗
我有一份字典清单,上面有学生的姓名、电子邮件和学生号。你知道吗
我想做的是,如果有60个学生,我想给他们一个唯一的学号。这一部分在elif语句中起作用。我不知道如何实现的部分是:
如果有20个学生,我想给例如2个学生相同的学生号码(这听起来很愚蠢,但我需要它)。在这种情况下,student是键,studentnumber是值。你知道吗
我现在拥有:
# student
[{"Student1":029182,"e-mail":"example@.com"},{"Student2":0591238,"e-mail":"example@.com"},{"Student3":9213189,"e-mail":"example@.com"}]
studentnumbers = [{"studentnumber":"029182","ID":0},{"studentnumber":"0591238","ID":1},{"studentnumber":"9213189","ID":2}]
students = [list(data.values())[0] for data in studentnumbers]
ID = 0
# students is a list of studentnumbers
if len(students) <= 20:
# studentInfo is my dictionary with the key and values
for data in studentnumbers:
try:
pass
except IndexError:
pass
elif len(students) <= 60:
for data in studentnumbers:
try:
studentnumbers[ID] = students[ID]
ID += 1
break
except IndexError:
pass
print("..")
else:
print("...")
with open('Students.json','w') as instudent:
json.dump(studentnumbers,instudent,indent=2)
输出我现在拥有的:
[{"Student1":029182,"e-mail":"example@.com","studentnumber":029182},
{"Student2":0591238,"e-mail":"example@.com","studentnumber":9213189},
{"Student3":9213189,"e-mail":"example@.com","studentnumber":0591238},
{"Student4":9213189,"e-mail":"example@.com","studentnumber":0294832},
{"Student5":9213189,"e-mail":"example@.com","studentnumber":0591823},
{"Student6":9213189,"e-mail":"example@.com","studentnumber":0501852}]
我想要的是:
[{"Student1":029182,"e-mail":"example@.com","studentnumber":029182},
{"Student2":0591238,"e-mail":"example@.com","studentnumber":029182},
{"Student3":9213189,"e-mail":"example@.com","studentnumber":029182},
{"Student4":9213189,"e-mail":"example@.com","studentnumber":9213189},
{"Student5":9213189,"e-mail":"example@.com","studentnumber":9213189},
{"Student6":9213189,"e-mail":"example@.com","studentnumber":9213189}]
似乎您想有条件地更新一个dict列表。你知道吗
给定的
代码
演示
一小部分学生(
n <= 20
)返回具有相同学生编号的dict:较大的列表(
n > 20
)返回带有随机学生编号的dict*:*注意:根据需要实现随机数生成器。这个函数从样本总体返回随机整数。
我个人不喜欢使用字典,我只是认为类对象更方便,尽管在大规模应用中可能更慢。你知道吗
我不知道你的问题是什么,我不知道字典里不可能有两个相同价值的东西。你知道吗
我会这样做的
如果你想设置学号,只需改变r.randint位
如果你要用字典的话,希望对你有帮助 如果您有任何问题,请询问:)
可能下面的代码能帮上忙。我得弄明白你不能用025这样的整数。所以我把它改成了弦。你知道吗
相关问题 更多 >
编程相关推荐