1 / 17

Sets

Sets. Intro to Computer Science CS1510 Dr. Sarah Diesburg. Homework. Any lingering questions about the homework?. Sets, as in Mathematical Sets. In mathematics, a set is a collection of objects, potentially of many different types.

tavon
Télécharger la présentation

Sets

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Sets Intro to Computer Science CS1510 Dr. Sarah Diesburg

  2. Homework • Any lingering questions about the homework?

  3. Sets, as in Mathematical Sets • In mathematics, a set is a collection of objects, potentially of many different types. • In a set, no two elements are identical. That is, a set consists of elements each of which is unique compared to the other elements. • There is no order to the elements of a set • A set with no elements is the empty set

  4. Creating a Set mySet = set(“abcd”) • The “set” keyword creates a set. • The single argument that follows must be iterable, that is, something that can be walked through one item at a time with a for. • The result is a set data structure: print(mySet) {'a', 'c', 'b', 'd'}

  5. Diverse Elements • A set can consist of a mixture of different types of elements: mySet = {‘a’,1,3.14159,True} • As long as the single argument can be iterated through, you can make a set of it.

  6. No Duplicates • Duplicates are automatically removed. mySet = set(“aabbccdd”) print(mySet) {'a', 'c', 'b', 'd‘}

  7. Common Operators Most data structures respond to these: • len(mySet) • the number of elements in a set • element in mySet • boolean indicating whether element is in the set • for element in mySet: • iterate through the elements in mySet

  8. Set Operators • The set data structure provides some special operators that correspond to the operators you learned in middle school. • These are various combinations of set contents.

  9. mySet=set(“abcd”); newSet=set(“cdef”) Set Ops, Union a b c d e f mySet.union(newSet) mySet | newSet returns {‘a’,’b’,‘c’,’d’,’e’,’f’}

  10. mySet=set(“abcd”); newSet=set(“cdef”) Set Ops, Intersection a b c d e f mySet.intersection(newSet) mySet & newSet returns {‘c’,’d’}

  11. mySet=set(“abcd”); newSet=set(“cdef”) Set Ops, Difference a bc d e f mySet.difference(newSet) mySet – newSet returns {‘a’,’b’}

  12. mySet=set(“abcd”); newSet=set(“cdef”) Set Ops, symmetricDifference a bc d e f mySet.symmetric_difference(newSet) mySet ^ newSet returns {‘a’,’b’,’e’,’f’}

  13. mySet=set(“abc”); newSet=set(“abcdef”) Set Ops, super and sub set a b c d e f mySet.issubset(newSet) mySet <= newSet returns True

  14. mySet=set(“abc”); newSet=set(“abcdef”) Set Ops, super and sub set a b c d e f newSet.issuperset(mySet) newSet>= mySet returns True

  15. Other Set Ops • mySet.add(“g”) • Adds to the set, no effect if item is in set already. • mSet.clear() • Empties the set. • mySet.remove(“g”) versus mySet.discard(“g”) • remove throws an error if “g” isn’t there. discard doesn’t care. Both remove “g” from the set. • mySet.copy() • Returns a shallow copy of mySet.

  16. Copy vs. Assignment mySet=set(“abc”) myCopy=mySet.copy() myRefCopy=mySet mySet.remove(‘b’) print myCopy print myRefCopy mySet {‘a’,‘c’} myRefCopy {‘a’,‘b’,‘c’} myCopy

  17. A Useful Example • Common/Unique Words • Code Listings 8.9-8.12

More Related