code_junkie

Comparing List of Arguments to it self?

Nov 12th, 2011
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. a = ['foo', 'foo', 'boo'] #not valid
  2. b = ['foo', 'foo', 'foo'] #valid
  3.  
  4. if a.count( "foo" ) != len(a)
  5.  
  6. if a.count( a[0] ) != len(a)
  7.  
  8. all(a[0] == x for x in a)
  9.  
  10. List Size 10
  11. 0.00530 aList.count(aList[0] ) == len(aList)
  12. 0.00699 for with return False if no match found.
  13. 0.00892 aList == [aList[0]] * len(aList)
  14. 0.00974 len(set(aList)) == 1
  15. 0.02334 all(aList[0] == x for x in aList)
  16. 0.02693 reduce(lambda x,y:x==y and x,aList)
  17.  
  18. List Size 100
  19. 0.01547 aList.count(aList[0] ) == len(aList)
  20. 0.01623 aList == [aList[0]] * len(aList)
  21. 0.03525 for with return False if no match found.
  22. 0.05122 len(set(aList)) == 1
  23. 0.08079 all(aList[0] == x for x in aList)
  24. 0.22797 reduce(lambda x,y:x==y and x,aList)
  25.  
  26. List Size 1000
  27. 0.09198 aList == [aList[0]] * len(aList)
  28. 0.11862 aList.count(aList[0] ) == len(aList)
  29. 0.31874 for with return False if no match found.
  30. 0.36145 len(set(aList)) == 1
  31. 0.65861 all(aList[0] == x for x in aList)
  32. 2.24386 reduce(lambda x,y:x==y and x,aList)
  33.  
  34. def quickExit( aList ):
  35. """for with return False if no match found."""
  36. value= aList[0]
  37. for x in aList:
  38. if x != value: return False
  39. return True
  40.  
  41. if len(set(my_list)) != 1:
  42. return False
  43.  
  44. all_items_are_same = len(set(my_list)) == 1
  45.  
  46. # Equality returns True or False
  47. all_items_are_same = (len(set(my_list)) == 1)
  48.  
  49. b == [b[0]] * len(b) #valid
  50. a == [a[0]] * len(a) #not valid
  51.  
  52. >>> a = ['foo', 'foo', 'boo'] #not valid
  53. >>> b = ['foo', 'foo', 'foo'] #valid
  54. >>> reduce(lambda x,y:x==y and x,a)
  55. False
  56. >>> reduce(lambda x,y:x==y and x,b)
  57. 'foo'
Add Comment
Please, Sign In to add comment