Python-Ref > Data structures > Sets > Set union, intersection and other functions
 
 

<-^^->
Klíčová slova
Moduly
Knihovní funkce

Set union, intersection and other functions

Expand/Shrink
Zdroj: (sets5-1.py)
  1   set1 = set( [1,2,3,8,9])
  2   set2 = set( [5,6,8,3])
  3   
  4   # union - elements from both sets
  5   print set1 | set2
  6   
  7   # intersection - common elements
  8   print set1 & set2
  9   
 10   # symmetric difference (xor)
 11   print set1 ^ set2
stdout:
set([1, 2, 3, 5, 6, 8, 9])
set([8, 3])
set([1, 2, 5, 6, 9])
Doba běhu: 49.6 ms
Expand/Shrink
Zdroj: (sets5-2.py)
  1   set1 = set( [1,2,3,8,9])
  2   set2 = set( [5,6,8,3])
  3   
  4   # difference
  5   print set1 - set2
  6   print set2 - set1
  7   
  8   print (set1-set2) & (set2-set1)
  9   print (set1-set2) | (set2-set1)
 10   print (set1-set2) | (set2-set1) | (set1 & set2)
 11   print set1 | set2
 12   
 13   print (set1-set2) | (set2-set1) == set1 ^ set2
 14   print (set1 | set2) - (set1 & set2) == set1 ^ set2
stdout:
set([1, 2, 9])
set([5, 6])
set([])
set([1, 2, 5, 6, 9])
set([1, 2, 3, 5, 6, 8, 9])
set([1, 2, 3, 5, 6, 8, 9])
True
True
Doba běhu: 18.8 ms
Expand/Shrink
Zdroj: (sets5-3.py)
  1   set1 = set( [1,2,3,8,9])
  2   set2 = set( [8,3])
  3   
  4   print set1 >= set2
  5   print set2 < set1
  6   print set2 > set1
  7   print set2.issubset( set1)
  8   print set2.issuperset( set1)
  9   print "----------"
 10   print set1 >= set1
 11   print set1 > set1
stdout:
True
True
False
True
False
----------
True
False
Doba běhu: 20.7 ms