赞
踩
生成假人配对
1.随机设置假人的姓名、性别(2代表男生,3代表女生)、城市、薪水。采用split+通过random函数实现
2.采用循环了1000个假人,生成采用了列表嵌套字典及列表中存放了1000条字典数据
4.通过双重循环随机配对,配对成功即break(破出循环),其它复杂情况留着以后玩
5.在配对过程中要注意男-女配对的问题,将(1-sex)即可得到需要的另外一个性别
6.格式化输出配对成功的假人
7.输出配对成功的人数
步奏如下
ChoiceCity = city1[int(random() * len(city1))] Sex = [0, 1] ChoiceSex = Sex[int(random() * len(Sex))] # print(ChoiceSex) ChoiceAge = randint(18, 60) # salary 收入 ChoiceSalary = randint(20000, 400000) #print(ChoiceSalary) ChoiceSalary1 = randint(20000, 400000) person = {"name": ChoiceName, "City": ChoiceCity, "Age": ChoiceAge, "Salary": ChoiceSalary, "Sex": ChoiceSex, "aim": ChoiceCity + "," + str(ChoiceSalary) + "," + str(1 - ChoiceSex)} Candidate.append(person)
count = 0
for i in Candidate:
for j in Candidate:
if j == i:
continue
else:
if i[“aim”].split(",")[0] == j[“City”] and int(i[“aim”].split(",")[1])>int(j[“Salary”]) and int(i[“aim”].split(",")[2]) == j[“Sex”] :
print(“姓名:{}与姓名:{}配对成功”.format(i[“name”],j[“name”]))
count +=1
break
print("%d配对成功"%count)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。