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/sets101.py (+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: mp+24611@code.launchpad.net

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/sets101.py'
2--- pythoncore/sets101.py 1970-01-01 00:00:00 +0000
3+++ pythoncore/sets101.py 2010-05-03 20:20:51 +0000
4@@ -0,0 +1,93 @@
5+#!/usr/bin/env python
6+#
7+# [SNIPPET_NAME: Sets 101]
8+# [SNIPPET_CATEGORIES: Python Core]
9+# [SNIPPET_DESCRIPTION: Basic and not so basic set operations]
10+# [SNIPPET_AUTHOR: Bruno Girin <brunogirin@gmail.com>]
11+# [SNIPPET_LICENSE: GPL]
12+
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.
17+
18+#
19+# First, let's create simple list. A list is an ordered collection of items
20+# and can have duplicates.
21+#
22+print "Create a simple list"
23+simpleList = ["Karmic", "Lucid", "Hardy", "Jaunty", "Intrepid", "Lucid"]
24+# print it
25+print simpleList
26+#
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.
30+#
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)
36+#
37+# Interrogate the set
38+#
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
48+#
49+# Modify the set
50+#
51+print "\nModify the set"
52+# add another item
53+simpleSet.add("Maverick")
54+print simpleSet
55+# remove an item
56+simpleSet.remove("Intrepid")
57+print simpleSet
58+#
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.
62+#
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)
97+

Subscribers

People subscribed via source and target branches