fork download
  1. from itertools import combinations
  2. if __name__ == '__main__':
  3. global B,C
  4.  
  5. B = []
  6. C = []
  7. D = []
  8.  
  9. list = [(0,1),(1,2),(2,1),(3,1),(4,0),(5,0),(6,3)]
  10.  
  11. def complete(x,y):
  12. check = True
  13. if int(x[1])+int(y[1]) == 2:
  14. if B:
  15. for value in B:
  16. if x in value or y in value:
  17. check = False
  18. return False
  19. if check:
  20. return True
  21. else: #如果B為空,第一組一定進去
  22. return True
  23. else:
  24. return False
  25.  
  26. def completeA(x,y):
  27. check = True
  28. if B: #已經分配給B的不能再次分組
  29. for value in B:
  30. if x in value or y in value:
  31. check = False
  32. return False
  33. if check:
  34. if int(x[1])+int(y[1]) == 1:
  35. if C:
  36. for value in C: #已經分好組的不能重複分組
  37. if x in value or y in value:
  38. check = False
  39. return False
  40. if check:
  41. return True
  42. else: #如果C為空,第一組一定進去
  43. return True
  44. else:
  45. return False
  46. else:
  47. return False
  48. else:
  49. return False
  50.  
  51. #B = [ c for c in combinations(list,2) if complete(c[0],c[1]) ]
  52. for c in combinations(list,2):
  53. if complete(c[0],c[1]):
  54. B.append(c)
  55.  
  56. for d in combinations(list,2):
  57. if completeA(d[0],d[1]):
  58. C.append(d)
  59.  
  60.  
  61. for i in list:
  62. check = True
  63. for valueB in B:
  64. if i in valueB:
  65. check = False
  66. for valueC in C:
  67. if i in valueC:
  68. check = False
  69. if check:
  70. D.append(i)
  71.  
  72.  
  73. #C = [ d for d in combinations(list,2) if completeA(d[0],d[1]) and d not in B]
  74.  
  75. print("list=",list)
  76. print("B=",B)
  77. print("C=",C)
  78. print("D=",D)
  79.  
  80.  
  81.  
  82.  
Success #stdin #stdout 0.02s 44680KB
stdin
Standard input is empty
stdout
('list=', [(0, 1), (1, 2), (2, 1), (3, 1), (4, 0), (5, 0), (6, 3)])
('B=', [((0, 1), (2, 1)), ((1, 2), (4, 0))])
('C=', [((3, 1), (5, 0))])
('D=', [(6, 3)])