Python‎ > ‎

set

http://www.adilwaseem.com/

Definition
The set create an unordered collection of unique elements.

Syntax
set() -> new empty set object
set(iterable) -> new set object

Parameters
iterable

Examples
The method return with the common elements of set.
>>> set1={4,5,6,7}
>>> set2={6,7,8,9,10}
>>> set1.intersection(set2)
>>>
{6, 7}
>>> 

__eq__
The method tested whether the sets are equal are not.
 >>> set1={4,5,6,7}
>>> set2={6,7,8,9,10}
>>> set1.__eq__(set2)
False

__and__
The method is equal to set.intersection.
>>> set1={4,5,6,7}
>>> set2={6,7,8,9,10}
 >>> set1.__and__(set2)
{6, 7}

__contains__
The method tested whether the element is contained in the set1.
>>> set1={4,5,6,7}
 >>> set1.__contains__(5)
True

__gt__
The method tested whether the set1 is greater than  set2.
>>> set1={4,5,6,7}
>>> set2={6,7,8,9,10} 
>>> set1.__gt__(set2)
False

__len__
The method return with:
>>> set1={4,5,6,7}
 >>> set1.__len__()
4

len(set)
Equivalent to:
>>> set1={4,5,6,7}
 >>> len(set1)
4
>>> 

__ne__
The method is set is not equal to set2.
 >>> set1={4,5,6,7}
>>> set2={6,7,8,9,10} 
>>> set1. __ne__(set2)
True
>>>

__lt__
The method tested whether the set1 is lesser than set2.
 >>> set1={4,5,6,7}
>>> set2={6,7,8,9,10} 
 >>> set1.__lt__(set2)
False

__or__
The method return with two equation comparison.
>>> set1={4,5,6,7}
>>> set2={6,7,8,9,10}
>>> set3={1,3,5,7} 
>>> set1==set3.__or__(set2==set3)
False
>>>

__rand__
The method return with the common elements between set1 and set2
>>> set1={4,5,6,7}
>>> set2={6,7,8,9,10} 
>>> set1.__rand__(set2)
{6, 7}
>>>

operator.length_hint(set)
The return is:
 >>>import operator
>>> x={2,5,6,7,8,8}
5
>>> x={2,5,6,7,8}
5
>>> len(x)
5

str.join(set)
Returns are following:
 >>> list=["Python", "Research", "Centre"]
>>> "Lake".join(list)
'PythonLakeResearchLakeCentre'
>>> set={"Pyhton", "Research", "Centre"}
>>> "Lake".join(set)
'ResearchLakePyhtonLakeCentre'
>>> tuple=("Pyhton", "Research", "Centre")
>>> "Lake".join(tuple)
'PyhtonLakeResearchLakeCentre'
>>> list==set
False
>>> set==tuple
False
>>> False==False
True
>>> "Lake".join(tuple)=="Lake".join(set)
False
>>> 'PyhtonLakeResearchLakeCentre'=='ResearchLakePyhtonLakeCentre'
False
>>> 'PythonLakeResearchLakeCentre'=='PythonLakeResearchLakeCentre'
True
>>>


for statement
Method run the loop for sets.
import math
set1={math.trunc(1.1),math.sqrt(49),math.pow(4,2),math.e,math.exp(44)}
set2={math.floor(3.12),math.fmod(3,2),math.fabs(22), math.ceil(1)}
for i in map(lambda a,b: a+b, set1, set2):
print(i)

>>> 
17.0
4
24.718281828459045
>>>


for loop in itertools.accumulate(set.difference)
The returns with accumulate of items of difference between sets.

from decimal import Decimal
import itertools
set1={Decimal(0.1).to_integral_value(),Decimal(1.9).to_integral_exact(),Decimal(1.5000001).to_integral(), Decimal(10).shift(2)}
set2={Decimal(1).scaleb(0), Decimal(1).remainder_near(2),Decimal(1.0000).normalize()}


x=set1.difference(set2)

y=itertools.accumulate(x)
for value in y:
print(value)

>>>
1000
1000
1002
>>>



if min(map(lambda(decimal,decimal)).__gt__()
The method returns with the minimum value in the sets equation set1/set2*set2 is greater than 2. 
from decimal import*
set1={Decimal(-1355).adjusted(),Decimal(-233.321).copy_negate(), Decimal(-23.99).exp()}
set2={ Decimal(2.0).fma(3,7),Decimal(1.1).log10()}
if min(map(lambda a,b: a/b*b, set1, set2)).__gt__(2):
        print("Return is greater than 2.")
else:
        print("Not within parameters")

>>> 
Return is greater than 2.
>>>       


itertools.accumulate(set)
The return with accumulation of set.
 import itertools
set={"Python", "Lake", "learning", "Center"}
y=itertools.accumulate(set)
for value in y: print(value)

>>>
learning
learningLake
learningLakeCenter
learningLakeCenterPython
>>>


The returns are following:
from decimal import Decimal
import itertools

set1={Decimal(0.1).to_integral_value(),Decimal(1.9).to_integral_exact(),Decimal(1.5000001).to_integral(), Decimal(10).shift(2)}
set2={Decimal(1).scaleb(0),Decimal(1).remainder_near(2),Decimal(1.0000).normalize()}

print("The decimal values of set1 are:", set1)
print("The decimal values of set2 are: ", set2)

max_set1=int(max({Decimal(0.1).to_integral_value(),Decimal(1.9).to_integral_exact(),
Decimal(1.5000001).to_integral(), Decimal(10).shift(2)}))
print("The maximum integer value in set1 is: ",max_set1)

min_set2=int(min({Decimal(1).scaleb(0),Decimal(1).remainder_near(2),Decimal(1.0000).normalize()}))
print("The minimum integer value in set2 is:",min_set2)

set3=max_set1.__add__(min_set2)
print("The set1 added by set2 is: ", set3)

set4=max_set1.__lt__(min_set2)
print("The max_set1 is larger than min_set2:", set4)

x=set1.difference(set2)
print("The set1 difference to set2 is :", x)

>>>
The decimal values of set1 are: {Decimal('1000'), Decimal('0'), Decimal('2')}
The decimal values of set2 are: {Decimal('1')}
The maximum integer value in set1 is: 1000
The minimum integer value in set2 is: 1
The set1 added by set2 is: 1001
The max_set1 is larger than min_set2: False
The set1 difference to set2 is : {Decimal('1000'), Decimal('0'), Decimal('2')}
>>>