fork download
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. my_list = [(0,1),(1,0),(2,1),(3,0),(4,1),(5,3),(6,3)]
  5.  
  6. # remove the comment to sort the list by id if you need
  7. #my_list.sort()
  8.  
  9. def select(my_list, k):
  10.  
  11. # used to check the sum of two value equals to k
  12. chk = {}
  13.  
  14. # used to store the candidates which pass the check with the data structure (item_1, item_2)
  15. tmp = []
  16. for item in my_list:
  17.  
  18. if k - item[1] in chk and chk[k - item[1]]:
  19. tmp.append((chk[k - item[1]].pop(), item))
  20. else:
  21. if item[1] in chk:
  22. chk[item[1]].append(item)
  23. else:
  24. chk[item[1]] = [item]
  25.  
  26. reminder = [item for value in chk.values() for item in value]
  27. return tmp, reminder
  28.  
  29. B, my_list = select(my_list, 2)
  30. C, D = select(my_list, 1)
  31.  
  32. print "B =", B
  33. print "C =", C
  34. print "D =", D
Success #stdin #stdout 0.01s 8968KB
stdin
Standard input is empty
stdout
B = [((0, 1), (2, 1))]
C = [((3, 0), (4, 1))]
D = [(1, 0), (5, 3), (6, 3)]