我有2个JSON文件。带有学生编号的1个文件和带有学生编号的1个文件。
我有一个词典列表,其中包含姓名,电子邮件和学生人数。
我想做的是,如果有60个学生,我想给他们一个唯一的学生编号。该部分在elif语句中起作用。我不知道如何实现的部分是:
如果有20个学生,我想给2个学生提供相同的学生号(听起来很愚蠢,但我需要)。在这种情况下,学生是关键,而学生号是值。
我现在所拥有的:
# 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}]
答案 0 :(得分:2)
您似乎想有条件地更新字典列表。
给出
import random
students = [
{"Student1": "029182", "e-mail": "ex@mail.com"},
{"Student2": "0591238", "e-mail": "ex@mail.com"},
{"Student3": "9213189", "e-mail": "ex@mail.com"},
]
代码
def get_unique_numbers(size=10):
"""Return an a list of unique random values."""
return random.sample(range(10000, 99999), size)
def update(students):
"""Return a list of updated dicts."""
n = len(students)
# Conditionally, generate student numbers
if n <= 20:
student_numbers = [students[0]["Student1"]] * n
elif 20 < n <= 60:
student_numbers = get_unique_numbers(n)
# Update dicts w/student numbers
return [{**d, "student_num": n} for d, n in zip(students, student_numbers)]
演示
一小部分学生(n <= 20
)返回具有相同学生编号的字典:
update(students)
# [{'Student1': '029182', 'e-mail': 'ex@mail.com', 'student_num': '029182'},
# {'Student2': '0591238', 'e-mail': 'ex@mail.com', 'student_num': '029182'},
# {'Student3': '9213189', 'e-mail': 'ex@mail.com', 'student_num': '029182'}]
更大的列表(n > 20
)返回带有随机学生编号的字典*:
update(students * 7)
# {'Student1': '029182', 'e-mail': 'ex@mail.com', 'student_num': 28308},
# {'Student2': '0591238', 'e-mail': 'ex@mail.com', 'student_num': 21986},
# {'Student3': '9213189', 'e-mail': 'ex@mail.com', 'student_num': 36603},
# ...
# {'Student1': '029182', 'e-mail': 'ex@mail.com', 'student_num': 38362},
# {'Student2': '0591238', 'e-mail': 'ex@mail.com', 'student_num': 99305},
# {'Student3': '9213189', 'e-mail': 'ex@mail.com', 'student_num': 78360}
**注:随心所欲地实现随机数生成器。这将从样本总体中返回随机整数。
答案 1 :(得分:1)
我个人不喜欢使用字典,我只是认为类对象更方便,尽管在大规模上可能更慢。
我不确定您的问题是什么,我不知道字典中不能有两个相同价值的东西。
这是我要怎么做
import random as r
class student():
def __init__(self,ID,numb):
self.number = ID
self.email = 'example@mail.com'
self.name = 'student{}'.format(numb)
students = []
for i in range(60):
x = student(r.randint(10000,99999),i)
students.append(x)
def test():
ID = 0
for i in list(students):
print(i.name,i.number,i.email)
test()
如果要设置学生编号,只需更改r.randint位p>
希望它对您必须使用字典有帮助 或者如果您有任何问题,请询问:)
答案 2 :(得分:1)
下面的代码可能会有所帮助。我必须弄清楚您不能使用像025这样的int数字。所以我将其更改为字符串。
# student numbers
studentNunbers = [{"studentnumber": '029182', "ID": 0}, {"studentnumber": '0591238',"ID": 1},{"studentnumber": '9213189', "ID":2}]
# student
studentInfos = [{"Student1":'029182', "e-mail":'example@.com'}, {"Student2":'0591238',"e-mail":'example@.com'},
{"Student3":'9213189',"e-mail":'example@.com'}, {"Student3":'92189',"e-mail":'example@.com'},
{"Student2":'0538',"e-mail":'example@.com'}, {"Student2":'238',"e-mail":'example@.com'},
{"Student2":'0598',"e-mail":'example@.com'}, {"Student2":'08',"e-mail":'example@.com'},]
students = [list(data.values())[0] for data in studentNunbers]
def merge(numRep=1):
key = "studentnumber"
stNumberIndex = 0
iterInd = numRep
for i in range(len(studentInfos)):
value = studentNunbers[stNumberIndex][key]
studentInfos[i][key] = value
iterInd -= 1
if iterInd == 0:
iterInd = numRep
stNumberIndex += 1
def test():
if len(studentNunbers) < 20:
merge(numRep=3)
elif len(studentNunbers) < 60:
merge(numRep=1)
else:
pass
test()
print(studentInfos)