python-Day3-set 集合
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python-Day3-set 集合相关的知识,希望对你有一定的参考价值。
上节内容回顾:
C语言为什么比起他语言块,因为C 会把代码变异成机器码
Pyhton 的 .pyc文件是什么
python 把.py文件编译成的.pyc文件是Python的字节码,
字符串本质是 字符数组,
python 一切事物都是对象,对象是类创建的,像 增加删除更改 都存在于类里边,也可以称作类的成员
set集合
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 """ 11 Add an element to a set. 12 13 This has no effect if the element is already present. 14 """ 15 pass 16 17 def clear(self, *args, **kwargs): # real signature unknown 18 """ Remove all elements from this set. """ 19 pass 20 21 def copy(self, *args, **kwargs): # real signature unknown 22 """ Return a shallow copy of a set. """ 23 pass 24 25 def difference(self, *args, **kwargs): # real signature unknown 26 """ 27 Return the difference of two or more sets as a new set. 28 29 (i.e. all elements that are in this set but not the others.) 30 """ 31 pass 32 33 def difference_update(self, *args, **kwargs): # real signature unknown 34 """ 删除当前set中的所有包含在 new set 里的元素 """ 35 """ Remove all elements of another set from this set. """ 36 pass 37 38 def discard(self, *args, **kwargs): # real signature unknown 39 """ 移除元素 """ 40 """ 41 Remove an element from a set if it is a member. 42 43 If the element is not a member, do nothing. 44 """ 45 pass 46 47 def intersection(self, *args, **kwargs): # real signature unknown 48 """ 取交集,新创建一个set """ 49 """ 50 Return the intersection of two or more sets as a new set. 51 52 (i.e. elements that are common to all of the sets.) 53 """ 54 pass 55 56 def intersection_update(self, *args, **kwargs): # real signature unknown 57 """ 取交集,修改原来set """ 58 """ Update a set with the intersection of itself and another. """ 59 pass 60 61 def isdisjoint(self, *args, **kwargs): # real signature unknown 62 """ 如果没有交集,返回true """ 63 """ Return True if two sets have a null intersection. """ 64 pass 65 66 def issubset(self, *args, **kwargs): # real signature unknown 67 """ 是否是子集 """ 68 """ Report whether another set contains this set. """ 69 pass 70 71 def issuperset(self, *args, **kwargs): # real signature unknown 72 """ 是否是父集 """ 73 """ Report whether this set contains another set. """ 74 pass 75 76 def pop(self, *args, **kwargs): # real signature unknown 77 """ 移除 """ 78 """ 79 Remove and return an arbitrary set element. 80 Raises KeyError if the set is empty. 81 """ 82 pass 83 84 def remove(self, *args, **kwargs): # real signature unknown 85 """ 移除 """ 86 """ 87 Remove an element from a set; it must be a member. 88 89 If the element is not a member, raise a KeyError. 90 """ 91 pass 92 93 def symmetric_difference(self, *args, **kwargs): # real signature unknown 94 """ 差集,创建新对象""" 95 """ 96 Return the symmetric difference of two sets as a new set. 97 98 (i.e. all elements that are in exactly one of the sets.) 99 """ 100 pass 101 102 def symmetric_difference_update(self, *args, **kwargs): # real signature unknown 103 """ 差集,改变原来 """ 104 """ Update a set with the symmetric difference of itself and another. """ 105 pass 106 107 def union(self, *args, **kwargs): # real signature unknown 108 """ 并集 """ 109 """ 110 Return the union of sets as a new set. 111 112 (i.e. all elements that are in either set.) 113 """ 114 pass 115 116 def update(self, *args, **kwargs): # real signature unknown 117 """ 更新 """ 118 """ Update a set with the union of itself and others. """ 119 pass 120 121 def __and__(self, y): # real signature unknown; restored from __doc__ 122 """ x.__and__(y) <==> x&y """ 123 pass 124 125 def __cmp__(self, y): # real signature unknown; restored from __doc__ 126 """ x.__cmp__(y) <==> cmp(x,y) """ 127 pass 128 129 def __contains__(self, y): # real signature unknown; restored from __doc__ 130 """ x.__contains__(y) <==> y in x. """ 131 pass 132 133 def __eq__(self, y): # real signature unknown; restored from __doc__ 134 """ x.__eq__(y) <==> x==y """ 135 pass 136 137 def __getattribute__(self, name): # real signature unknown; restored from __doc__ 138 """ x.__getattribute__(‘name‘) <==> x.name """ 139 pass 140 141 def __ge__(self, y): # real signature unknown; restored from __doc__ 142 """ x.__ge__(y) <==> x>=y """ 143 pass 144 145 def __gt__(self, y): # real signature unknown; restored from __doc__ 146 """ x.__gt__(y) <==> x>y """ 147 pass 148 149 def __iand__(self, y): # real signature unknown; restored from __doc__ 150 """ x.__iand__(y) <==> x&=y """ 151 pass 152 153 def __init__(self, seq=()): # known special case of set.__init__ 154 """ 155 set() -> new empty set object 156 set(iterable) -> new set object 157 158 Build an unordered collection of unique elements. 159 # (copied from class doc) 160 """ 161 pass 162 163 def __ior__(self, y): # real signature unknown; restored from __doc__ 164 """ x.__ior__(y) <==> x|=y """ 165 pass 166 167 def __isub__(self, y): # real signature unknown; restored from __doc__ 168 """ x.__isub__(y) <==> x-=y """ 169 pass 170 171 def __iter__(self): # real signature unknown; restored from __doc__ 172 """ x.__iter__() <==> iter(x) """ 173 pass 174 175 def __ixor__(self, y): # real signature unknown; restored from __doc__ 176 """ x.__ixor__(y) <==> x^=y """ 177 pass 178 179 def __len__(self): # real signature unknown; restored from __doc__ 180 """ x.__len__() <==> len(x) """ 181 pass 182 183 def __le__(self, y): # real signature unknown; restored from __doc__ 184 """ x.__le__(y) <==> x<=y """ 185 pass 186 187 def __lt__(self, y): # real signature unknown; restored from __doc__ 188 """ x.__lt__(y) <==> x<y """ 189 pass 190 191 @staticmethod # known case of __new__ 192 def __new__(S, *more): # real signature unknown; restored from __doc__ 193 """ T.__new__(S, ...) -> a new object with type S, a subtype of T """ 194 pass 195 196 def __ne__(self, y): # real signature unknown; restored from __doc__ 197 """ x.__ne__(y) <==> x!=y """ 198 pass 199 200 def __or__(self, y): # real signature unknown; restored from __doc__ 201 """ x.__or__(y) <==> x|y """ 202 pass 203 204 def __rand__(self, y): # real signature unknown; restored from __doc__ 205 """ x.__rand__(y) <==> y&x """ 206 pass 207 208 def __reduce__(self, *args, **kwargs): # real signature unknown 209 """ Return state information for pickling. """ 210 pass 211 212 def __repr__(self): # real signature unknown; restored from __doc__ 213 """ x.__repr__() <==> repr(x) """ 214 pass 215 216 def __ror__(self, y): # real signature unknown; restored from __doc__ 217 """ x.__ror__(y) <==> y|x """ 218 pass 219 220 def __rsub__(self, y): # real signature unknown; restored from __doc__ 221 """ x.__rsub__(y) <==> y-x """ 222 pass 223 224 def __rxor__(self, y): # real signature unknown; restored from __doc__ 225 """ x.__rxor__(y) <==> y^x """ 226 pass 227 228 def __sizeof__(self): # real signature unknown; restored from __doc__ 229 """ S.__sizeof__() -> size of S in memory, in bytes """ 230 pass 231 232 def __sub__(self, y): # real signature unknown; restored from __doc__ 233 """ x.__sub__(y) <==> x-y """ 234 pass 235 236 def __xor__(self, y): # real signature unknown; restored from __doc__ 237 """ x.__xor__(y) <==> x^y """ 238 pass 239 240 __hash__ = None 241 242 set
集合里不允许重复的元素存在
对象是由类创建的
要创建一个set
创建一个 set无序集合
列表有两种创建方法:
a1 = []
a2 = list()
set 通过类创建对象、
s1 = set() 这就是创建了一个集合的对象
现在可以往里边添加对象
1 __author__ = ‘Administrator‘ 2 # -*- coding:utf-8 -*- 3 #定义一个空的集合 4 s1 = set() 5 #给集合添加对象 6 s1.add(‘amd‘) 7 #打印集合 8 print(s1) 9 #打印类型 10 print(type(s1)) 11 ----------------------------------------------------------------------------------- 12 #打印添加对象后的集合 13 {‘amd‘} 14 #打印显示所属类型为集合 15 <class ‘set‘>
以上是关于python-Day3-set 集合的主要内容,如果未能解决你的问题,请参考以下文章