Advertisement
homer512

tuple compare

May 13th, 2014
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.62 KB | None | 0 0
  1. #!/usr/bin/python2
  2.  
  3.  
  4. def variant_1(tups, front):
  5.     for tup in tups:
  6.         if tup[0] == front:
  7.             print tup
  8.  
  9.  
  10. def variant_2(tups, front):
  11.     lookup = {tup[0]: tup for tup in tups}
  12.     try:
  13.         print lookup[front]
  14.     except KeyError:
  15.         pass
  16.  
  17.  
  18. def variant_3(tups, front):
  19.     filtered = [tup for tup in tups if tup[0] == front]
  20.     print filtered
  21.  
  22.  
  23. def main():
  24.     tups = [((1, 2), 'word1', 1), ((2, 2), 'word2', 2), ((3, 2), 'word3', 3)]
  25.     front = (3, 2)
  26.     for variant in (variant_1, variant_2, variant_3):
  27.         variant(tups, front)
  28.  
  29.  
  30. if __name__ == '__main__':
  31.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement