最近遇到一个问题,要求0-9 十个数的排列组合
例如:
6758912034
1203467589
8956712034
1203489567
6759812034
程序源码:
#!/usr/bin/env python
a=[0,1,2,3,4]
b=[5,6,7,8,9]
little=[a]
lage=[b]
for i in range(0,5):
for j in range(0,5):
aa=a[:]
bb=b[:]
k=a[i]
aa.remove(a[i])
aa.append(b[j])
bb.remove(b[j])
bb.append(k)
little.append(aa)
lage.append(bb)
for i in range(0,5):
for ii in range(i+1,5):
for j in range(0,5):
for jj in range(j+1,5):
aa=a[:]
bb=b[:]
k=a[i]
kk=a[ii]
aa.remove(a[i])
aa.remove(a[ii])
aa.append(b[j])
aa.append(b[jj])
bb.remove(b[j])
bb.remove(b[jj])
bb.append(k)
bb.append(kk)
little.append(aa)
lage.append(bb)
def paixu(number):
l=[]
for m in range(0,len(number)):
llm=[]
for i in range(0,5):
mm=number[m][:]
mm.remove(number[m][i])
ma=[[mm[0],mm[1]],[mm[2],mm[1]],[mm[3],mm[1]]]
mb=[[mm[2],mm[3]],[mm[0],mm[3]],[mm[2],mm[0]]]
for j in range(0,3):
k=[ma[j][0],ma[j][1],number[m][i],mb[j][0],mb[j][1]]
llm.append(k)
k=[mb[j][0],mb[j][1],number[m][i],ma[j][0],ma[j][1]]
llm.append(k)
k=[ma[j][0],ma[j][1],number[m][i],mb[j][1],mb[j][0]]
llm.append(k)
k=[mb[j][1],mb[j][0],number[m][i],ma[j][0],ma[j][1]]
llm.append(k)
k=[ma[j][1],ma[j][0],number[m][i],mb[j][0],mb[j][1]]
llm.append(k)
k=[mb[j][0],mb[j][1],number[m][i],ma[j][1],ma[j][0]]
llm.append(k)
k=[ma[j][1],ma[j][0],number[m][i],mb[j][1],mb[j][0]]
llm.append(k)
k=[mb[j][1],mb[j][0],number[m][i],ma[j][1],ma[j][0]]
llm.append(k)
l.append(llm)
return(l)
ll = paixu(little)
lg = paixu(lage)
#print ll[0],ll[1]
#count=[]
f = open('test.dat', 'w')
for i in range(0,126):
for m in ll[i]:
for n in lg[i]:
#icount.append([m,n])
#count.append([n,m])
f.write("%s%s%s%s%s%s%s%s%s%s\n" % (n[0],n[1],n[2],n[3],n[4],m[0],m[1],m[2],m[3],m[4]))
f.write("%s%s%s%s%s%s%s%s%s%s\n" % (m[0],m[1],m[2],m[3],m[4],n[0],n[1],n[2],n[3],n[4]))
#print len(count)
运行结果
初学python写的简陋,忘各位大牛指点。
转载于:https://blog.51cto.com/linux521/826779