Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikibooksThe Free Textbook Project
Search

Python Programming/Sets

From Wikibooks, open books for an open world
<Python Programming
Previous: DictionariesIndexNext: Operators


Starting with version 2.3, Python comes with an implementation of the mathematical set. Initially this implementation had to be imported from the standard moduleset, but with Python 2.6 the types set andfrozenset became built-in types. A set is an unordered collection of objects, unlike sequence objects such as lists and tuples, in which each element is indexed. Sets cannot have duplicate members - a given object appears in a set 0 or 1 times. All members of a set have to be hashable, just like dictionary keys. Integers, floating point numbers, tuples, and strings are hashable; dictionaries, lists, and other sets (except frozensets) are not.

Overview

[edit |edit source]

Sets in Python at a glance:

set1=set()# A new empty setset1.add("cat")# Add a single memberset1.update(["dog","mouse"])# Add several members, like list's extendset1|=set(["doe","horse"])# Add several members 2, like list's extendif"cat"inset1:# Membership testset1.remove("cat")#set1.remove("elephant") - throws an errorset1.discard("elephant")# No error thrownprint(set1)foriteminset1:# Iteration AKA for each elementprint(item)print("Item count:",len(set1))# Length AKA size AKA item count#1stitem = set1[0]             # Error: no indexing for setsisempty=len(set1)==0# Test for emptinessset1={"cat","dog"}# Initialize set using braces; since Python 2.7#set1 = {}                     # No way; this is a dictset1=set(["cat","dog"])# Initialize set from a listset2=set(["dog","mouse"])set3=set1&set2# Intersectionset4=set1|set2# Unionset5=set1-set3# Set differenceset6=set1^set2# Symmetric differenceissubset=set1<=set2# Subset testissuperset=set1>=set2# Superset testset7=set1.copy()# A shallow copyset7.remove("cat")print(set7.pop())# Remove an arbitrary elementset8=set1.copy()set8.clear()# Clear AKA empty AKA eraseset9={xforxinrange(10)ifx%2}# Set comprehension; since Python 2.7print(set1,set2,set3,set4,set5,set6,set7,set8,set9,issubset,issuperset)

Constructing Sets

[edit |edit source]

One way to construct sets is by passing any sequential object to the "set" constructor.

>>>set([0,1,2,3])set([0,1,2,3])>>>set("obtuse")set(['b','e','o','s','u','t'])

We can also add elements to sets one by one, using the "add" function.

>>>s=set([12,26,54])>>>s.add(32)>>>sset([32,26,12,54])

Note that since a set does not contain duplicate elements, if we add one of the members of s to s again, the add function will have no effect. This same behavior occurs in the "update" function, which adds a group of elements to a set.

>>>s.update([26,12,9,14])>>>sset([32,9,12,14,54,26])

Note that you can give any type of sequential structure, or even another set, to the update function, regardless of what structure was used to initialize the set.

The set function also provides a copy constructor. However, remember that the copy constructor will copy the set, but not the individual elements.

>>>s2=s.copy()>>>s2set([32,9,12,14,54,26])

Membership Testing

[edit |edit source]

We can check if an object is in the set using the same "in" operator as with sequential data types.

>>>32insTrue>>>6insFalse>>>6notinsTrue

We can also test the membership of entire sets. Given two setsS1{\displaystyle S_{1}} andS2{\displaystyle S_{2}}, we check ifS1{\displaystyle S_{1}} is asubset or a superset ofS2{\displaystyle S_{2}}.

>>>s.issubset(set([32,8,9,12,14,-4,54,26,19]))True>>>s.issuperset(set([9,12]))True

Note that "issubset" and "issuperset" can also accept sequential data types as arguments

>>>s.issuperset([32,9])True

Note that the <= and >= operators also express the issubset and issuperset functions respectively.

>>>set([4,5,7])<=set([4,5,7,9])True>>>set([9,12,15])>=set([9,12])True

Like lists, tuples, and string, we can use the "len" function to find the number of items in a set.

Removing Items

[edit |edit source]

There are three functions which remove individual items from a set, called pop, remove, and discard. The first, pop, simply removes an item from the set. Note that there is no defined behavior as to which element it chooses to remove.

>>>s=set([1,2,3,4,5,6])>>>s.pop()1>>>sset([2,3,4,5,6])

We also have the "remove" function to remove a specified element.

>>>s.remove(3)>>>sset([2,4,5,6])

However, removing a item which isn't in the set causes an error.

>>>s.remove(9)Traceback(mostrecentcalllast):File"<stdin>",line1,in?KeyError:9

If you wish to avoid this error, use "discard." It has the same functionality as remove, but will simply do nothing if the element isn't in the set

We also have another operation for removing elements from a set, clear, which simply removes all elements from the set.

>>>s.clear()>>>sset([])

Iteration Over Sets

[edit |edit source]

We can also have a loop move over each of the items in a set. However, since sets are unordered, it is undefined which order the iteration will follow.

>>>s=set("blerg")>>>fornins:...print(n,"",end="")...rbelg

Set Operations

[edit |edit source]

Python allows us to perform all the standard mathematical set operations, using members of set. Note that each of these set operations has several forms. One of these forms, s1.function(s2) will return another set which is created by "function" applied toS1{\displaystyle S_{1}} andS2{\displaystyle S_{2}}. The other form, s1.function_update(s2), will changeS1{\displaystyle S_{1}} to be the set created by "function" ofS1{\displaystyle S_{1}} andS2{\displaystyle S_{2}}. Finally, some functions have equivalent special operators. For example, s1 & s2 is equivalent to s1.intersection(s2)

Intersection

[edit |edit source]

Any element which is in bothS1{\displaystyle S_{1}} andS2{\displaystyle S_{2}} will appear in theirintersection.

>>>s1=set([4,6,9])>>>s2=set([1,6,8])>>>s1.intersection(s2)set([6])>>>s1&s2set([6])>>>s1.intersection_update(s2)>>>s1set([6])

Union

[edit |edit source]

Theunion is the merger of two sets. Any element inS1{\displaystyle S_{1}} orS2{\displaystyle S_{2}} will appear in their union.

>>>s1=set([4,6,9])>>>s2=set([1,6,8])>>>s1.union(s2)set([1,4,6,8,9])>>>s1|s2set([1,4,6,8,9])

Note that union's update function is simply "update"above.

Symmetric Difference

[edit |edit source]

Thesymmetric difference of two sets is the set of elements which are in one of either set, but not in both (also called exclusive-or in logic).

>>>s1=set([4,6,9])>>>s2=set([1,6,8])>>>s1.symmetric_difference(s2)set([8,1,4,9])>>>s1^s2set([8,1,4,9])>>>s1.symmetric_difference_update(s2)>>>s1set([8,1,4,9])

Set Difference

[edit |edit source]

Python can also find theset difference ofS1{\displaystyle S_{1}} andS2{\displaystyle S_{2}}, which is the elements that are inS1{\displaystyle S_{1}} but not inS2{\displaystyle S_{2}}.

>>>s1=set([4,6,9])>>>s2=set([1,6,8])>>>s1.difference(s2)set([9,4])>>>s1-s2set([9,4])>>>s1.difference_update(s2)>>>s1set([9,4])

Multiple sets

[edit |edit source]

Starting with Python 2.6, "union", "intersection", and "difference" can work with multiple input. For example, using "set.intersection()":

>>>s1=set([3,6,7,9])>>>s2=set([6,7,9,10])>>>s3=set([7,9,10,11])>>>set.intersection(s1,s2,s3)set([9,7])

frozenset

[edit |edit source]

A frozenset is basically the same as a set, except that it is immutable - once it is created, its members cannot be changed. Since they are immutable, they are also hashable, which means that frozensets can be used as members in other sets and as dictionary keys. frozensets have the same functions as normal sets, except none of the functions that change the contents (update, remove, pop, etc.) are available.

>>>fs=frozenset([2,3,4])>>>s1=set([fs,4,5,6])>>>s1set([4,frozenset([2,3,4]),6,5])>>>fs.intersection(s1)frozenset([4])>>>fs.add(6)Traceback(mostrecentcalllast):File"<stdin>",line1,in<module>AttributeError:'frozenset'objecthasnoattribute'add'

Exercises

[edit |edit source]
  1. Create the set {'cat', 1, 2, 3}, call it s.
  2. Create the set {'c', 'a', 't', '1', '2', '3'}.
  3. Create the frozen set {'cat', 1, 2, 3}, call it fs.
  4. Create a set containing the frozenset fs, it should look like {frozenset({'cat', 2, 3, 1})}.

Reference

[edit |edit source]
Previous: DictionariesIndexNext: Operators
Retrieved from "https://en.wikibooks.org/w/index.php?title=Python_Programming/Sets&oldid=4404528"
Category:

[8]ページ先頭

©2009-2025 Movatter.jp