Experimenting with Sets (Python)

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
vegaseat vegaseat is offline Offline Mar 9th, 2005, 4:27 pm |
0
The set is an unordered collection of unique hashable elements. All the elements/items in a set are treated as keys. There are several functions that compare two sets. Right now, I will just give you a taste ...
Quick reply to this message  
Python Syntax
  1. # sets are unordered collections of unique hashable elements
  2. # Python23 tested vegaseat 09mar2005
  3.  
  4. # Python v2.4 has sets built in
  5. import sets
  6.  
  7. print "List the functions within module 'sets':"
  8. for funk in dir(sets):
  9. print funk
  10.  
  11. # create an empty set
  12. set1 = set([])
  13. # now load the set
  14. for k in range(10):
  15. set1.add(k)
  16. print "\nLoaded a set with 0 to 9:"
  17. print set1
  18. set1.add(7)
  19. print "Tried to add another 7, but it was already there:"
  20. print set1
  21.  
  22. # make a list of fruits as you put them into a basket
  23. basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
  24. print "\nThe original list of fruits:"
  25. print basket
  26.  
  27. # create a set from the list, removes the duplicates
  28. fruits = sets.Set(basket)
  29. print "\nThe set is unique, but the order has changed:"
  30. print fruits
  31.  
  32. # let's get rid of some duplicate words
  33. str1 = "Senator Strom Thurmond dressed as as Tarzan"
  34. print "\nOriginal string:"
  35. print str1
  36. print "A list of the words in the string:"
  37. wrdList1 = str1.split()
  38. print wrdList1
  39. # now create a set of unique words
  40. strSet = sets.Set(wrdList1)
  41. print "The set of the words in the string:"
  42. print strSet
  43. print "Convert set back to string (order has changed!):"
  44. print " ".join(strSet)
  45.  
  46. print
  47.  
  48. # comparing two sets, bear with me ...
  49. colorSet1 = sets.Set(['red','green','blue','black','orange','white'])
  50. colorSet2 = sets.Set(['black','maroon','grey','blue'])
  51. print "colorSet1 =", colorSet1
  52. print "colorSet2 =", colorSet2
  53.  
  54. # same as (colorSet1 - colorSet2)
  55. colorSet3 = colorSet1.difference(colorSet2)
  56. print "\nThese are the colors in colorSet1 that are not in colorSet2:"
  57. print colorSet3
  58.  
  59. # same as (colorSet1 | colorSet2)
  60. colorSet4 = colorSet1.union(colorSet2)
  61. print "\nThese are the colors appearing in both sets:"
  62. print colorSet4
  63.  
  64. # same as (colorSet1 ^ colorSet2)
  65. colorSet5 = colorSet1.symmetric_difference(colorSet2)
  66. print "\nThese are the colors in colorSet1 or in colorSet2, but not both:"
  67. print colorSet5
  68.  
  69. # same as (colorSet1 & colorSet2)
  70. colorSet6 = colorSet1.intersection(colorSet2)
  71. print "\nThese are the colors common to colorSet1 and colorSet2:"
  72. print colorSet6

Message:


Similar Threads
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC