Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
65674b80
Commit
65674b80
authored
Nov 18, 2003
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Documentation for set objects.
parent
1b92fd5b
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
37 additions
and
0 deletions
+37
-0
Doc/tut/tut.tex
Doc/tut/tut.tex
+37
-0
No files found.
Doc/tut/tut.tex
View file @
65674b80
...
...
@@ -2053,6 +2053,43 @@ always creates a tuple, and unpacking works for any sequence.
% XXX Add a bit on the difference between tuples and lists.
\section
{
Sets
\label
{
sets
}}
Python also includes a data type for
\emph
{
sets
}
. A set is an unordered
collection with no duplicate elements. Basic uses include membership
testing and eliminating duplicate entries. Set objects also support
mathematical operations like union, intersection, difference, and
symmetric difference.
Here is a brief demonstration:
\begin
{
verbatim
}
>>> basket
=
[
'apple', 'orange', 'apple', 'pear', 'orange', 'banana'
]
>>> fruits
=
set
(
basket
)
# create a set without duplicates
>>> fruits
set
([
'orange', 'pear', 'apple', 'banana'
])
>>> 'orange' in fruits # fast membership testing
True
>>> 'crabgrass' in fruits
False
>>> # Demonstrate set operations on unique letters from two words
...
>>> a
=
set
(
'abracadabra'
)
>>> b
=
set
(
'alacazam'
)
>>> a # unique letters in a
set
([
'a', 'r', 'b', 'c', 'd'
])
>>> a
-
b # letters in a but not in b
set
([
'r', 'd', 'b'
])
>>> a | b # letters in either a or b
set
([
'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'
])
>>> a
&
b # letters in both a and b
set
([
'a', 'c'
])
>>> a
^
b # letters in a or b but not both
set
([
'r', 'd', 'b', 'm', 'z', 'l'
])
\end
{
verbatim
}
\section
{
Dictionaries
\label
{
dictionaries
}}
Another useful data type built into Python is the
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment