首页 > 代码库 > python set

python set

set是一个工厂函数(filter也是工厂函数),是一个可变的集合

frozenset 不可变的集合,与set共性,他也在内部自动去重,

1 >>> num5=frozenset([1,1,1,2,3,])
2 >>> num5
3 frozenset([1, 2, 3])
4 >>> 

 

 

>>> num1=[1,2,3,4,5,5,4,3]
>>> tmp=[]
>>> for each in num1:
    if each not in tmp:
        tmp.
        
SyntaxError: invalid syntax
>>> for each int num1:
    
SyntaxError: invalid syntax
>>> for each in num1:
    if each not in tmp:
        tmp.append(each)

        
>>> num1
[1, 2, 3, 4, 5, 5, 4, 3]
>>> tmp
[1, 2, 3, 4, 5]
>>> num2=list(set(num1))
>>> num1
[1, 2, 3, 4, 5, 5, 4, 3]
>>> num2
[1, 2, 3, 4, 5]
>>> num3={1,2,3,4}
>>> type(num3)
<type set>
>>> 
>>> 1 in num3
True
>>> 7 in num3
False
>>> num3.add(6)
>>> num3
set([1, 2, 3, 4, 6])
>>> num3.remove(1)
>>> num3
set([2, 3, 4, 6])
>>> num3.remove(4)
>>> num3
set([2, 3, 6])
>>> 
>>> 
>>> num4=frozenst([1,2,3])

Traceback (most recent call last):
  File "<pyshell#168>", line 1, in <module>
    num4=frozenst([1,2,3])
NameError: name frozenst is not defined
>>> num4=frozenset([1,2,3])
>>> num4
frozenset([1, 2, 3])
>>> type(num4)
<type frozenset>
>>> num4[0]

Traceback (most recent call last):
  File "<pyshell#172>", line 1, in <module>
    num4[0]
TypeError: frozenset object does not support indexing
>>> 1 in num4
True
>>> 4 not in num4
True
>>> 4 in num4
False
>>> num4.add(1)

Traceback (most recent call last):
  File "<pyshell#176>", line 1, in <module>
    num4.add(1)
AttributeError: frozenset object has no attribute add
>>> help(frozenset)
Help on class frozenset in module __builtin__:

class frozenset(object)
 |  frozenset() -> empty frozenset object
 |  frozenset(iterable) -> frozenset object
 |  
 |  Build an immutable unordered collection of unique elements.
 |  
 |  Methods defined here:
 |  
 |  __and__(...)
 |      x.__and__(y) <==> x&y
 |  
 |  __cmp__(...)
 |      x.__cmp__(y) <==> cmp(x,y)
 |  
 |  __contains__(...)
 |      x.__contains__(y) <==> y in x.
 |  
 |  __eq__(...)
 |      x.__eq__(y) <==> x==y
 |  
 |  __ge__(...)
 |      x.__ge__(y) <==> x>=y
 |  
 |  __getattribute__(...)
 |      x.__getattribute__(name) <==> x.name
 |  
 |  __gt__(...)
 |      x.__gt__(y) <==> x>y
 |  
 |  __hash__(...)
 |      x.__hash__() <==> hash(x)
 |  
 |  __iter__(...)
 |      x.__iter__() <==> iter(x)
 |  
 |  __le__(...)
 |      x.__le__(y) <==> x<=y
 |  
 |  __len__(...)
 |      x.__len__() <==> len(x)
 |  
 |  __lt__(...)
 |      x.__lt__(y) <==> x<y
 |  
 |  __ne__(...)
 |      x.__ne__(y) <==> x!=y
 |  
 |  __or__(...)
 |      x.__or__(y) <==> x|y
 |  
 |  __rand__(...)
 |      x.__rand__(y) <==> y&x
 |  
 |  __reduce__(...)
 |      Return state information for pickling.
 |  
 |  __repr__(...)
 |      x.__repr__() <==> repr(x)
 |  
 |  __ror__(...)
 |      x.__ror__(y) <==> y|x
 |  
 |  __rsub__(...)
 |      x.__rsub__(y) <==> y-x
 |  
 |  __rxor__(...)
 |      x.__rxor__(y) <==> y^x
 |  
 |  __sizeof__(...)
 |      S.__sizeof__() -> size of S in memory, in bytes
 |  
 |  __sub__(...)
 |      x.__sub__(y) <==> x-y
 |  
 |  __xor__(...)
 |      x.__xor__(y) <==> x^y
 |  
 |  copy(...)
 |      Return a shallow copy of a set.
 |  
 |  difference(...)
 |      Return the difference of two or more sets as a new set.
 |      
 |      (i.e. all elements that are in this set but not the others.)
 |  
 |  intersection(...)
 |      Return the intersection of two or more sets as a new set.
 |      
 |      (i.e. elements that are common to all of the sets.)
 |  
 |  isdisjoint(...)
 |      Return True if two sets have a null intersection.
 |  
 |  issubset(...)
 |      Report whether another set contains this set.
 |  
 |  issuperset(...)
 |      Report whether this set contains another set.
 |  
 |  symmetric_difference(...)
 |      Return the symmetric difference of two sets as a new set.
 |      
 |      (i.e. all elements that are in exactly one of the sets.)
 |  
 |  union(...)
 |      Return the union of sets as a new set.
 |      
 |      (i.e. all elements that are in either set.)
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |  
 |  __new__ = <built-in method __new__ of type object>
 |      T.__new__(S, ...) -> a new object with type S, a subtype of T

>>> 

 

python set