computer basics
online shopping

introduction
programming language
environment setup
basic
data type
operator
condition & iteration
number
string
list
tuple
dictionary
set
date & time
function
module

 

Advanced Topics
file formats
-csv
-xml
-json
-ini
db connections
-oracle
-other db
lambda
exception handling
os
pandas
-assignment
-solution

 

set


A set is a well defined collection of objects.  The elements or members of a set can be anything: numbers, characters, words, names, letters of the alphabet, even other sets, and so on.

The data type "set", which is a collection type, contains an unordered collection of unique and immutable objects. The set data type is, as the name implies, a Python implementation of the sets as they are known from mathematics. This explains, why sets unlike lists or tuples can't have multiple occurrences of the same element. 

If we want to create a set, we can call the built-in set function with a sequence or another iterable object: 
In the following example, a string is singularized into its characters to build the resulting set x:
>>> x = set("A Python Tutorial")
>>> x
{'A', ' ', 'i', 'h', 'l', 'o', 'n', 'P', 'r', 'u', 't', 'a', 'y', 'T'}
>>> type(x)
<class 'set'>
 
We can pass a list to the built-in set function, as we can see in the following: 
>>> x = set(["Perl", "Python", "Java"])
>>> x
{'Python', 'Java', 'Perl'}
 
We want to show now, what happens, if we pass a tuple with reappearing elements to the set function - in our example the city "Paris": 
>>> cities = set(("Paris", "Lyon", "London","Berlin","Paris","Birmingham"))
>>> cities
{'Paris', 'Birmingham', 'Lyon', 'London', 'Berlin'}
As we have expected, no doublets are in the resulting set of cities.


 

Immutable Sets

Sets are implemented in a way, which doesn't allow mutable objects. The following example demonstrates that we cannot include for example lists as elements:
>>> cities = set((("Python","Perl"), ("Paris", "Berlin", "London")))
>>> cities = set((["Python","Perl"], ["Paris", "Berlin", "London"]))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'


 

Frozensets

Though sets can't contain mutable objects, sets are mutable: 
>>> cities = set(["Frankfurt", "Basel","Freiburg"])
>>> cities.add("Strasbourg")
>>> cities
{'Freiburg', 'Basel', 'Frankfurt', 'Strasbourg'}

 
Frozensets are like sets except that they cannot be changed, i.e. they are immutable:
>>> cities = frozenset(["Frankfurt", "Basel","Freiburg"])
>>> cities.add("Strasbourg")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'frozenset' object has no attribute 'add'

Operation

Equivalent

Result

len(s)

 

number of elements in set s (cardinality)

x in s

 

test x for membership in s

x not in s

 

test x for non-membership in s

s.issubset(t)

s <= t

test whether every element in s is in t

s.issuperset(t)

s >= t

test whether every element in t is in s

s.union(t)

s | t

new set with elements from both s and t

s.intersection(t)

s & t

new set with elements common to s and t

s.difference(t)

s - t

new set with elements in s but not in t

s.symmetric_difference(t)

s ^ t

new set with elements in either s or t but not both

s.copy()

 

new set with a shallow copy of s

 

Operation

Equivalent

Result

s.update(t)

s |= t

return set s with elements added from t

s.intersection_update(t)

s &= t

return set s keeping only elements also found in t

s.difference_update(t)

s -= t

return set s after removing elements found in t

s.symmetric_difference_update(t)

s ^= t

return set s with elements from s or t but not both

s.add(x)

 

add element x to set s

s.remove(x)

 

remove x from set s; raises KeyError if not present

s.discard(x)

 

removes x from set s if present

s.pop()

 

remove and return an arbitrary element from s; raises KeyError if empty

s.clear()

 

remove all elements from set s

 

>>> from sets import Set
>>> engineers = Set(['John', 'Jane', 'Jack', 'Janice'])
>>> programmers = Set(['Jack', 'Sam', 'Susan', 'Janice'])
>>> managers = Set(['Jane', 'Jack', 'Susan', 'Zack'])
>>> employees = engineers | programmers | managers # union
>>> engineering_management = engineers & managers # intersection
>>> fulltime_management = managers - engineers - programmers # difference
>>> engineers.add('Marvin') # add element
>>> print engineers
Set(['Jane', 'Marvin', 'Janice', 'John', 'Jack'])
>>> employees.issuperset(engineers) # superset test
False
>>> employees.update(engineers) # update from another set
>>> employees.issuperset(engineers)
True
>>> for group in [engineers, programmers, managers, employees]:
... group.discard('Susan') # unconditionally remove element
... print group
...
Set(['Jane', 'Marvin', 'Janice', 'John', 'Jack'])
Set(['Janice', 'Jack', 'Sam'])
Set(['Jane', 'Zack', 'Jack'])
Set(['Jack', 'Sam', 'Jane', 'Marvin', 'Janice', 'John', 'Zack'])

 

 

 

 
         
 
 
www.000webhost.com