Merge lp:~brunogirin/python-snippets/sets101 into lp:~jonobacon/python-snippets/trunk

Proposed by Bruno Girin
Status: Needs review
Proposed branch: lp:~brunogirin/python-snippets/sets101
Merge into: lp:~jonobacon/python-snippets/trunk
Diff against target: 97 lines (+93/-0)
1 file modified
pythoncore/ (+93/-0)
To merge this branch: bzr merge lp:~brunogirin/python-snippets/sets101
Reviewer Review Type Date Requested Status
Jono Bacon Pending
Review via email:

Description of the change

Added snippet that demonstrates set operators (union, intersection, etc).

To post a comment you must log in.

Unmerged revisions

100. By Bruno Girin

Added snippets to demonstrate set operators.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added file 'pythoncore/'
2--- pythoncore/ 1970-01-01 00:00:00 +0000
3+++ pythoncore/ 2010-05-03 20:20:51 +0000
4@@ -0,0 +1,93 @@
5+#!/usr/bin/env python
7+# [SNIPPET_NAME: Sets 101]
9+# [SNIPPET_DESCRIPTION: Basic and not so basic set operations]
10+# [SNIPPET_AUTHOR: Bruno Girin <>]
13+# This snippet demonstrates how the basics on sets: how to create sets, add,
14+# remove items, and use set operations.
15+# The set operators demonstrated in this snippet are courtesy of a very good
16+# article by Carl Chenet in the April 2010 French edition of Linux Magazine.
19+# First, let's create simple list. A list is an ordered collection of items
20+# and can have duplicates.
22+print "Create a simple list"
23+simpleList = ["Karmic", "Lucid", "Hardy", "Jaunty", "Intrepid", "Lucid"]
24+# print it
25+print simpleList
27+# Create a set from the list. A set is an un-ordered collection of items and
28+# cannot have duplicates. So creating a set from a list is a quick way to
29+# remove any duplicates. However, you lose the ordering when you do that.
31+simpleSet = set(simpleList)
32+# print it
33+print simpleSet
34+# order it based on the natural ordering of the elements and print it
35+print sorted(simpleSet)
37+# Interrogate the set
39+print "\nInterrogate the set"
40+# get the length of the set
41+print "Number of items in the set: ", len(simpleSet)
42+# is Maverick in the set?
43+print "Maverick is in the set:", ("Maverick" in simpleSet)
44+# iterate over the set
45+print "\nIterate over the set"
46+for v in simpleSet:
47+ print v
49+# Modify the set
51+print "\nModify the set"
52+# add another item
54+print simpleSet
55+# remove an item
57+print simpleSet
59+# Set operators
60+# In the code below, all sets are sorted when printed to make it easier to show
61+# what the operators do but you don't have to do this in actual code.
63+print "\nSet operators"
64+# create two sets
65+lts = set(["Dapper", "Hardy", "Lucid"])
66+recent = set(["Hardy", "Intrepid", "Jaunty", "Karmic", "Lucid"])
67+print "LTS:", sorted(lts)
68+print "Recent:", sorted(recent)
69+# set union
70+union = lts | recent
71+print sorted(union)
72+# set intersection
73+inter = lts & recent
74+print sorted(inter)
75+# symetrical difference (items in either set but not both)
76+sym = lts ^ recent
77+print sorted(sym)
78+# difference
79+diff = recent - lts
80+print sorted(diff)
81+# the operators above also have versions to update in the set in place
82+print "\nIn place set operators"
83+latest = set(["Karmic", "Lucid", "Maverick"])
84+print "Latest:", sorted(latest)
85+# union
86+union |= latest
87+print sorted(union)
88+# intersection
89+inter &= latest
90+print sorted(inter)
91+# symetrical difference
92+sym ^= latest
93+print sorted(sym)
94+# difference
95+diff -= latest
96+print sorted(diff)


People subscribed via source and target branches