a,b,c,d,e = '6087','5173','1358','3825','2531'
#创建一个'0000'到'9999'的列表
list9999= []
for q1 in range(0,10):
for q2 in range(0,10):
for q3 in range(0,10):
for q4 in range(0,10):
temp = str(q1)+str(q2)+str(q3)+str(q4)
list9999.append(temp)
#把满足有两位数字正确(不管位置)的组合放入列表
listkey=[]
for i in list9999:
counta,countb,countc,countd,counte = 0,0,0,0,0
for each_i in i:
if each_i in a:
counta += 1
if each_i in b:
countb += 1
if each_i in c:
countc += 1
if each_i in d:
countd += 1
if each_i in e:
counte += 1
if counta==countb==countc==countd==counte==2:
listkey.append(i)
for each_one in listkey[:]:
temp = str(each_one)
#逐一位比较,如果有相同则从列表剔除,剔除时有可能出现重复剔除,用了try
for n in range(0,4):
if temp[n] == a[n] or temp[n] == b[n] or temp[n] == c[n] or temp[n] == d[n] or temp[n] == e[n]:
try:
listkey.remove(each_one)
finally:
break
print(listkey)
答案是['8712'],,小白一个,方法很笨   |