回复 3楼 阿智兄
程序代码:
from itertools import combinations,permutations
m=["A0001:02,06,07,24,28,29","A0002:02,08,21,24,29,31","A0003:05,07,09,16,18,27","A0004:13,17,19,24,26,30","A0005:01,02,06,11,24,29","A0006:12,14,18,20,26,28","A0007:04,10,12,18,23,25","A0008:02,12,13,14,31,33","A0009:01,05,08,15,16,30","A0010:06,11,21,25,28,31","A0011:08,09,13,27,29,31","A0012:08,10,15,17,22,30","A0013:11,16,23,26,27,33","A0014:05,07,09,16,20,26","A0015:03,05,13,19,20,25","A0016:06,08,14,17,18,30","A0017:01,02,06,10,24,29","A0018:02,21,22,23,27,28","A0019:10,15,17,25,31,32","A0020:04,08,17,24,28,33","A0021:01,03,06,07,09,11","A0022:12,16,17,24,28,29","A0023:02,08,14,23,25,32","A0024:09,15,18,29,32,33","A0025:03,08,09,13,15,18","A0026:06,14,16,26,28,29","A0027:04,15,21,25,29,33","A0028:06,09,12,16,27,31","A0029:03,06,14,18,20,26","A0030:05,10,16,23,27,33"]
result=[]#存放所有的数字集合
txt={}#存放每一样的数据用于输出
copydict={}#存放是否已经获取过
for one in m :
onelist=one.split(':')[1].split(',') #获取每行的数字["02","06","07","24","28","29"]
result.append(onelist)#生成数据集[["02","06","07","24","28","29"],["02","06","07","24","28","29"]]
txt['-'.join(onelist)]=one#存放每行的数据对应的字典用于后续输出{"02-06-07-24-28-29"=>"A0001:02,06,07,24,28,29"}
result.reverse()
while result:
check=result.pop()#每次从result取出一组数据。因为是遍历所有组合,所以处理后不需要再次在下面做循环判断。所以用的pop
comblist=list(combinations(check,4))#获取C(6,4)
for comblist_one in comblist:
if '-'.join(comblist_one) in copydict:#如果是已经获取过的,跳过以免重复
continue
sign=0
for result_one in result:
if set(comblist_one).issubset(result_one):#通过set的issubset判断comblist_one是否是result_one的子集
sign=1
copydict['-'.join(comblist_one)]=1 #存入判断用的字典
print(txt['-'.join(result_one)],end='\t')#输出result里符合要求的行的信息
if sign==1:
print(txt['-'.join(check)],end='\t')#如果除了当前行符合要求还有其他符合要求的行,输出当前行信息
print(comblist_one,end='\t')#输出('02', '06', '24', '29')
print("")
A0017:01,02,06,10,24,29 A0005:01,02,06,11,24,29 A0001:02,06,07,24,28,29 ('02', '06', '24', '29')
A0014:05,07,09,16,20,26 A0003:05,07,09,16,18,27 ('05', '07', '09', '16')
A0017:01,02,06,10,24,29 A0005:01,02,06,11,24,29 ('01', '02', '06', '24')
A0017:01,02,06,10,24,29 A0005:01,02,06,11,24,29 ('01', '02', '06', '29')
A0017:01,02,06,10,24,29 A0005:01,02,06,11,24,29 ('01', '02', '24', '29')
A0017:01,02,06,10,24,29 A0005:01,02,06,11,24,29 ('01', '06', '24', '29')
A0029:03,06,14,18,20,26 A0006:12,14,18,20,26,28 ('14', '18', '20', '26')
A0030:05,10,16,23,27,33 A0013:11,16,23,26,27,33 ('16', '23', '27', '33')
[此贴子已经被作者于2021-4-23 09:29编辑过]