Python‎ > ‎set‎ > ‎

set.difference()

http://www.adilwaseem.com/

Definition
Return with difference of set items.

Syntax
set(x).difference(y)

Parameters
x, y=set items

Examples
The method returns with difference in set items.
 set1={3,45,344,66}
set2={1,44,66}
set3=set1.difference(set2)
print("set1 is different from set 2 and values are: ", set3)
>>> 
set1 is different from set 2 and values are: {344, 3, 45}
>>>

The method return the difference items between sets.
 >>> set1={'Centre', 'Python', 'Research'}
>>> set2={"Python", "Lake"}
>>> set1.difference(set2)
{'Centre', 'Research'}
>>> 

The method return with the difference in the set items:
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
>>>

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 lessor 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 lessor than min_set2: False
The set1 difference to set2 is : {Decimal('1000'), Decimal('0'), Decimal('2')}
>>>


set.difference()
The return with following:
 >>> from decimal import Decimal
>>> a=set()
>>> a.add(Decimal(1).adjusted())
>>> a.add(Decimal(.55).canonical())
>>> a.add(Decimal(-1).compare_signal(1))
>>> a
{0, Decimal('0.5500000000000000444089209850062616169452667236328125'), Decimal('-1')}
>>> b=set()
>>> b.add(Decimal(1).compare_total(2))
>>> b.add(Decimal(0.1).copy_abs())
>>> b
{Decimal('0.1000000000000000055511151231257827021181583404541015625'), Decimal('-1')}
>>> 
>>> a.difference(b)
{0, Decimal('0.5500000000000000444089209850062616169452667236328125')}

Comments