From 1380f2f47a7e658e87b5dd4ce205e0c4ca3caa68 Mon Sep 17 00:00:00 2001 From: Mark Pilgrim Date: Sat, 4 Jul 2009 00:35:42 -0400 Subject: [PATCH] some text in #sets --- native-datatypes.html | 91 +++++++++++++++++++++++++------------------ 1 file changed, 53 insertions(+), 38 deletions(-) diff --git a/native-datatypes.html b/native-datatypes.html index 50a2b35..901ec49 100644 --- a/native-datatypes.html +++ b/native-datatypes.html @@ -442,7 +442,11 @@ IndexError: pop from empty list

Sets

-

FIXME +

A set is an unordered “bag” of unique values. A single set can contain values of any datatype. Once you have a two sets, you can do standard set operations like union, intersection, and set difference. + +

Creating A Set

+ +

First things first. Creating a set is easy.

 >>> a_set = {1}     
@@ -454,12 +458,12 @@ IndexError: pop from empty list
>>> a_set {1, 2}
    -
  1. FIXME -
  2. -
  3. +
  4. To create a set with one value, put the value in curly brackets ({}). +
  5. Sets are actually implemented as classes, but don’t worry about that for now. +
  6. To create a set with multiple values, separate the values with commas and wrap it all up with curly brackets.
-

You can create a set out of a list. +

You can also create a set out of a list.

 >>> a_list = ['a', 'b', 'mpilgrim', True, False, 42]
@@ -469,12 +473,12 @@ IndexError: pop from empty list
>>> a_list ['a', 'b', 'mpilgrim', True, False, 42]
    -
  1. FIXME -
  2. -
  3. +
  4. To create a set from a list, use the set() function. (Pedants who know about how sets are implemented will point out that this is not really calling a function, but instantiating a class. I promise you will learn the difference later in this book. For now, just know that set() acts like a function, and it returns a set.) +
  5. As I mentioned earlier, a single set can contain values of any datatype. And, as I mentioned earlier, sets are unordered. This set does not remember the original order of the list that was used to create it. If you were to add items to this set, it would not remember the order in which you added them. +
  6. The original list is unchanged.
-

You can create an empty set. +

Don’t have any values yet? Not a problem. You can create an empty set.

 >>> a_set = set()    
@@ -485,38 +489,37 @@ IndexError: pop from empty list
>>> len(a_set) 0 >>> not_sure = {} ->>> type(not_sure) +>>> type(not_sure) <class 'dict'>
    -
  1. FIXME -
  2. -
  3. -
  4. -
  5. -
  6. +
  7. To create an empty set, call set() with no arguments. +
  8. The printed representation of an empty set looks a bit strange. Were you expecting {}, perhaps? That would denote an empty dictionary, not an empty set. (You’ll learn about dictionaries later in this chapter.) +
  9. Despite the strange printed representation, this is a set… +
  10. …and this set has no members. +
  11. Due to historical quirks carried over from Python 2, you can not create an empty set with two curly brackets. This actually creates an empty dictionary, not an empty set.

Modifying A Set

-

FIXME +

There are two different ways to add values to an existing set: the add() method, and the update() method.

 >>> a_set = {1, 2}
->>> a_set.add(3)  
+>>> a_set.add(4)  
 >>> a_set
-{1, 2, 3}
+{1, 2, 4}
 >>> len(a_set)    
 3
 >>> a_set.add(1)  
 >>> a_set
-{1, 2, 3}
+{1, 2, 4}
 >>> len(a_set)    
 3
    -
  1. FIXME -
  2. -
  3. -
  4. +
  5. The add() method takes a single argument, which can be any datatype, and adds the given value to the set. +
  6. This set now has 3 members. +
  7. Sets are bags of unique values. If you try to add a value that already exists in the set, it will do nothing. It won’t raise an error; it’s just a no-op. +
  8. This set still has 3 members.
@@ -524,23 +527,24 @@ IndexError: pop from empty list
>>> a_set {1, 2, 3} >>> a_set.update({2, 4, 6}) ->>> a_set +>>> a_set {1, 2, 3, 4, 6} ->>> a_set.update({3, 6, 9}, {1, 2, 3, 5, 8, 13}) +>>> a_set.update({3, 6, 9}, {1, 2, 3, 5, 8, 13}) >>> a_set {1, 2, 3, 4, 5, 6, 8, 9, 13} ->>> a_set.update([10, 20, 30]) +>>> a_set.update([10, 20, 30]) >>> a_set {1, 2, 3, 4, 5, 6, 8, 9, 10, 13, 20, 30}
    -
  1. FIXME -
  2. -
  3. +
  4. The update() method takes one argument, a set, and adds all its members to the original set. It’s as if you called the add() method with each member of the set. +
  5. Duplicate values are ignored, since sets can not contain duplicates. +
  6. You can actually call the update() method with any number of arguments. When called with two sets, the update() method adds all the members of each set to the original set (dropping duplicates). +
  7. The update() method can take a number of different datatypes, including lists. When called with a list, the update() method adds all the items of the list to the original set.

Removing Items From A Set

-

FIXME +

There are three ways to remove individual values from a set. The first two, discard() and remove(), have one subtle difference.

 >>> a_set = {1, 3, 6, 10, 15, 21, 28, 36, 45}
@@ -560,24 +564,35 @@ IndexError: pop from empty list
File "<stdin>", line 1, in <module> KeyError: 21
    -
  1. FIXME -
  2. -
  3. -
  4. +
  5. The discard() method takes a single value as an argument and removes that value from the set. +
  6. If you call the discard() method with a value that doesn’t exist in the set, it does nothing. No error; it’s just a no-op. +
  7. The remove() method also takes a single value as an argument, and it also removes that value from the set. +
  8. Here’s the difference: if the value doesn’t exist in the set, the remove() method raises a KeyError exception.
+

Like lists, sets have a pop() method. +

 >>> a_set = {1, 3, 6, 10, 15, 21, 28, 36, 45}
->>> a_set.pop()
+>>> a_set.pop()                                
 1
 >>> a_set.pop()
 3
 >>> a_set.pop()
 36
 >>> a_set
-{6, 10, 45, 15, 21, 28}
+{6, 10, 45, 15, 21, 28} +>>> a_set.clear() +>>> a_set +set() +>>> a_set.pop() +Traceback (most recent call last): + File "<stdin>", line 1, in <module> +KeyError: 'pop from an empty set'
    -
  1. FIXME +
  2. The pop() method removes a single value from a set a returns the value. However, since sets are unordered, there is no “last” value in a set, so there is no way to control which value gets removed. It is essentially random. +
  3. The clear() method removes all values from a set, leaving you with an empty set. This is equivalent to a_set = set(), which would create a new empty set and overwrite the previous value of the a_set variable. +
  4. Attempting to pop a value from an empty set will raise a KeyError exception.

Common Set Operations