Advertisement
skip420

inverse

Sep 5th, 2022 (edited)
773
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.47 KB | None | 0 0
  1. # inverse
  2.  
  3.  
  4. from collections import Counter
  5. from itertools import repeat
  6.  
  7. def remove_opposite_pairs(seq):
  8.     dplus = Counter(seq)
  9.     dminus = Counter(-x for x in seq)
  10.     for x,n in (dplus - dminus).items():
  11.         yield from repeat(x, n)
  12.  
  13. lsts = [[326, -326, 336, -336, 336],
  14.         [-35, 35, 838, -838, 440],
  15.         [544, -544, 544],
  16.         [-25, 25, 32, -32, -32, 56, 79],
  17.         [-1, 0, 1, -1, -1]]
  18.  
  19. for l in lsts:
  20.     print(list(remove_opposite_pairs(l)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement