基础数据类型-set

Set是无序不重复元素的序列,基本功能是删除重复元素和测试成员关系,

创建一个集合可以用set()或者({}),但是创建一个空集合只能用set():

s1 = set()
print("s1 is", type(s1))
s2 = ({1, 2, 3})
print("s2 is ", type(s2))
s3 = ({})
print("s3 is", type(s3))
View Code
s1 is <class 'set'>
s2 is  <class 'set'>
s3 is <class 'dict'>
View Result

功能,删除序列里重复元素,测试成员关系,集合运算:

 1 #删除序列里重复元素
 2 fruits1 = set({"Apple", "Orange", "Pear", "Apple"})
 3 print("fruits1:", fruits1)
 4 
 5 #成员测试
 6 if 'Pear' in fruits1:
 7     print("Pear is in set")
 8 else:
 9     print("Pear is not in set")
10 
11 #成员运算
12 fruits2 = set({"Apple", "Banana", "Strawberry"})
13 print(fruits1 - fruits2) #在fruits1中但不再fruits2中
14 print(fruits1 | fruits2) #在fruits1或fruits2中
15 print(fruits1 & fruits2) #在fruits1且在fruits2中
16 print(fruits1 ^ fruits2) #在fruits1或fruits2中,但不同时在两个集合中
Code
1 fruits1: {'Apple', 'Orange', 'Pear'}
2 Pear is in set
3 {'Orange', 'Pear'}
4 {'Apple', 'Orange', 'Strawberry', 'Banana', 'Pear'}
5 {'Apple'}
6 {'Strawberry', 'Banana', 'Orange', 'Pear'}
Result

Set常用方法有add(), difference(),intersection()等:

1 #set常用方法
2 fruits2.add("Watermelon")
3 print("fruits2:", fruits2)
4 print("Fruits in fruits2 but not in fruits1 are:", fruits2.difference(fruits1)) #fruits2中有fruits1中没有
5 print("Fruits in fruits1 but not in fruits2 are:", fruits1.difference(fruits2)) #fruits1中有fruits2中没有
6 print("Fruits in fruits1 and fruits2 are:", fruits2.intersection(fruits1)) #fruits1,fruits2的交集
Code
1 fruits2: {'Strawberry', 'Banana', 'Apple', 'Watermelon'}
2 Fruits in fruits2 but not in fruits1 are: {'Strawberry', 'Banana', 'Watermelon'}
3 Fruits in fruits1 but not in fruits2 are: {'Pear', 'Orange'}
4 Fruits in fruits1 and fruits2 are: {'Apple'}
Result

Set类的定义-其他不常用方法查询:

  1 class set(object):
  2     """
  3     set() -> new empty set object
  4     set(iterable) -> new set object
  5     
  6     Build an unordered collection of unique elements.
  7     """
  8     def add(self, *args, **kwargs): # real signature unknown
  9         """
 10         Add an element to a set.
 11         
 12         This has no effect if the element is already present.
 13         """
 14         pass
 15 
 16     def clear(self, *args, **kwargs): # real signature unknown
 17         """ Remove all elements from this set. """
 18         pass
 19 
 20     def copy(self, *args, **kwargs): # real signature unknown
 21         """ Return a shallow copy of a set. """
 22         pass
 23 
 24     def difference(self, *args, **kwargs): # real signature unknown
 25         """
 26         Return the difference of two or more sets as a new set.
 27         
 28         (i.e. all elements that are in this set but not the others.)
 29         """
 30         pass
 31 
 32     def difference_update(self, *args, **kwargs): # real signature unknown
 33         """ Remove all elements of another set from this set. """
 34         pass
 35 
 36     def discard(self, *args, **kwargs): # real signature unknown
 37         """
 38         Remove an element from a set if it is a member.
 39         
 40         If the element is not a member, do nothing.
 41         """
 42         pass
 43 
 44     def intersection(self, *args, **kwargs): # real signature unknown
 45         """
 46         Return the intersection of two sets as a new set.
 47         
 48         (i.e. all elements that are in both sets.)
 49         """
 50         pass
 51 
 52     def intersection_update(self, *args, **kwargs): # real signature unknown
 53         """ Update a set with the intersection of itself and another. """
 54         pass
 55 
 56     def isdisjoint(self, *args, **kwargs): # real signature unknown
 57         """ Return True if two sets have a null intersection. """
 58         pass
 59 
 60     def issubset(self, *args, **kwargs): # real signature unknown
 61         """ Report whether another set contains this set. """
 62         pass
 63 
 64     def issuperset(self, *args, **kwargs): # real signature unknown
 65         """ Report whether this set contains another set. """
 66         pass
 67 
 68     def pop(self, *args, **kwargs): # real signature unknown
 69         """
 70         Remove and return an arbitrary set element.
 71         Raises KeyError if the set is empty.
 72         """
 73         pass
 74 
 75     def remove(self, *args, **kwargs): # real signature unknown
 76         """
 77         Remove an element from a set; it must be a member.
 78         
 79         If the element is not a member, raise a KeyError.
 80         """
 81         pass
 82 
 83     def symmetric_difference(self, *args, **kwargs): # real signature unknown
 84         """
 85         Return the symmetric difference of two sets as a new set.
 86         
 87         (i.e. all elements that are in exactly one of the sets.)
 88         """
 89         pass
 90 
 91     def symmetric_difference_update(self, *args, **kwargs): # real signature unknown
 92         """ Update a set with the symmetric difference of itself and another. """
 93         pass
 94 
 95     def union(self, *args, **kwargs): # real signature unknown
 96         """
 97         Return the union of sets as a new set.
 98         
 99         (i.e. all elements that are in either set.)
100         """
101         pass
102 
103     def update(self, *args, **kwargs): # real signature unknown
104         """ Update a set with the union of itself and others. """
105         pass
Class Set

 

posted on 2017-01-18 16:08  _Joshua  阅读(227)  评论(0编辑  收藏  举报

导航