注:查看对象相关成员 var,type,dir
一、整数
如: 18、73、84
每一个整数都具备如下功能
1 bit_length(self): 2 """ 返回表示该数字的时占用的二进制最少位数 """ 3 a = 18 4 print(bin(a)) 5 print(a.bit_length()) 6 0b10010 7 5 8 最少占用5位二进制数 9 可以通过 bin(a) 查看二进制 10 11 例: 12 arg = 18 13 print(arg.bit_length()) 14 print(bin(arg)) 15 输出; 16 5 17 0b10010 18 0b 表示二进制 19 20
1 conjugate(self, *args, **kwargs): # real signature unknown 2 """ 返回该复数的共轭复数 """ 3 pass 4
1 __abs__(self): 2 """ 返回绝对值 """ 3 """ x.__abs__() <==> abs(x) """ 4 pass 5 a = -18 6 print(a.__abs__()) 7 打印:18 8 python 会把常用的功能放在内置函数里 9 也可以通过abs(-19)来调用__abs__()来拿到绝对值
1 __add__(self, y): 2 """ x.__add__(y) <==> x+y """ 3 pass 4 两个值相加 5 a = -18 6 print(a.__add__(100)) 7 打印:82
1 __and__(self, y): 2 """ x.__and__(y) <==> x&y """ 3 pass 4 与运算 5 1 且 2 6
1 __cmp__(self, y): 2 """ 比较两个数大小 """ 3 """ x.__cmp__(y) <==> cmp(x,y) """ 4 pass 5 6 在 Python3 中 已经没有了
1 def __bool__(self, *args, **kwargs): # real signature unknown 2 """ self != 0 """ 3 pass 4 布尔值的转换
1 def __divmod__(self, y): 2 """ 相除,得到商和余数组成的元组 """ 3 """ x.__divmod__(y) <==> divmod(x, y) """ 4 pass 5 6 a = 18 7 print(a.__divmod__(10)) 8 9 (1, 8) 10 -------------------- 11 例如: 12 all_item = 95 13 pager = 10 14 resual = all_item.__divmod__(10) 15 print(resual) 16 输出: 17 (9, 5) 18 ------------------------------ 19 a = 18 20 b = a.__divmod__(10) 21 if b[1] >0: 22 print("da le ") 23 else: 24 print("xiaole") 25 print(b[0])
1 _eq__(self, *args, **kwargs): # real signature unknown 2 """ Return self==value. """ 3 pass 4 #两个 字符串做对比如果相等 输出 True 否则 输出 Fales 5 age = 1 6 agr = 2 7 ww = age.__eq__(1) 8 print(ww) 9 10 True 11 -------------------------- 12 age = 1 13 agr = 2 14 ww = age.__eq__(2) 15 print(ww) 16 17 False
1 浮点型 把一个数字转换成浮点型 2 age = 1 3 #执行了一个age.__float__() 是吧age 转换成浮点型可以通过print 类型来查看 4 eee = age.__float__() 5 print(type(age)) 6 print(type(eee)) 7 8 <class 'int'> 9 <class 'float'>
1 __floordiv__(self, *args, **kwargs): # real signature unknown 2 """ Return self//value. """ 3 pass 4 5 地板除 6 7 age = 5 8 #表示打印age 地板出 6 得出的结果是 0 9 print(age.__floordiv__(6)) 10 #也可以用 print(a//6) 11 12 0
1 def __ge__(self, *args, **kwargs): # real signature unknown 2 """ Return self>=value. """ 3 pass 4 #如果age = 18 那么 age.__ge__(99),self(表示age,18)>=value(value表示 传进来的99) 5 #如果age 大于等于等于 17 则 显示True,如果age小于99则显示Fale 6 7 age = 18 8 9 ww = age.__ge__(99) 10 print(ww) 11 输出:Fasle 12 ------------------------- 13 age = 18 14 15 ww = age.__ge__(17) 16 print(ww) 17 输出:True
1 def __gt__(self, *args, **kwargs): # real signature unknown 2 """ Return self>value. """ 3 pass 4 #如果age = 18 那么 age.__gt__(99),self(表示age,18)>value(value表示 传进来的99) 5 #如果age 大于等于 17 则 显示True,如果age小于99则显示Fale 6 agr = 18 7 8 ww = agr.__gt__(17) 9 print(ww) 10 输出: True 11 ----------------------------- 12 agr = 18 13 14 ww = agr.__gt__(99) 15 print(ww) 16 输出: False
1 def __hash__(self, *args, **kwargs): # real signature unknown 2 """ Return hash(self). """ 3 pass 4 #哈希值 系统创建对象的时候自动生成的哈希值
1 def __index__(self, *args, **kwargs): # real signature unknown 2 """ Return self converted to an integer, if self is suitable for use as an index into a list. """ 3 pass 4 #索引
1 def __init__(self, x, base=10): # known special case of int.__init__ 2 """ 3 int(x=0) -> integer 4 int(x, base=10) -> integer 5 6 Convert a number or string to an integer, or return 0 if no arguments 7 are given. If x is a number, return x.__int__(). For floating point 8 numbers, this truncates towards zero. 9 10 If x is not a number or if base is given, then x must be a string, 11 bytes, or bytearray instance representing an integer literal in the 12 given base. The literal can be preceded by '+' or '-' and be surrounded 13 by whitespace. The base defaults to 10. Valid bases are 0 and 2-36. 14 Base 0 means to interpret the base from the string as an integer literal. 15 >>> int('0b100', base=0) 16 4 17 # (copied from class doc) 18 """ 19 pass 20 # 一个类里面有__init__叫做构造方法, 21 如果; 22 age = 19 23 age = int(19) 24 #在python 解释器里 执行int类的时候回自动触发__init__ 方法
1 def __invert__(self, *args, **kwargs): # real signature unknown 2 """ ~self """ 3 pass 4 #位运算 5 def __le__(self, *args, **kwargs): # real signature unknown 6 """ Return self<=value. """ 7 pass 8 9 def __lshift__(self, *args, **kwargs): # real signature unknown 10 """ Return self<<value. """ 11 pass 12 13 def __lt__(self, *args, **kwargs): # real signature unknown 14 """ Return self<value. """ 15 pass 16 17 def __mod__(self, *args, **kwargs): # real signature unknown 18 """ Return self%value. """ 19 pass 20 21 def __mul__(self, *args, **kwargs): # real signature unknown 22 """ Return self*value. """ 23 pass 24 25 def __neg__(self, *args, **kwargs): # real signature unknown 26 """ -self """ 27 pass 28 29 @staticmethod # known case of __new__ 30 def __new__(*args, **kwargs): # real signature unknown 31 """ Create and return a new object. See help(type) for accurate signature. """ 32 pass 33 34 def __ne__(self, *args, **kwargs): # real signature unknown 35 """ Return self!=value. """ 36 pass
1 #或 or 相等的 2 def __or__(self, *args, **kwargs): # real signature unknown 3 """ Return self|value. """ 4 pass
1 def __pos__(self, *args, **kwargs): # real signature unknown 2 """ +self """ 3 pass 4
1 #幂 2 def __pow__(self, *args, **kwargs): # real signature unknown 3 """ Return pow(self, value, mod). """ 4 pass 5 例; 6 agr = 2 7 print(agr.__pow__(8)) 8 #等同于 2**8
上面举例写了一些,详细地全面地请看下面
1 class int(object): 2 """ 3 int(x=0) -> int or long 4 int(x, base=10) -> int or long 5 6 Convert a number or string to an integer, or return 0 if no arguments 7 are given. If x is floating point, the conversion truncates towards zero. 8 If x is outside the integer range, the function returns a long instead. 9 10 If x is not a number or if base is given, then x must be a string or 11 Unicode object representing an integer literal in the given base. The 12 literal can be preceded by '+' or '-' and be surrounded by whitespace. 13 The base defaults to 10. Valid bases are 0 and 2-36. Base 0 means to 14 interpret the base from the string as an integer literal. 15 >>> int('0b100', base=0) 16 """ 17 def bit_length(self): 18 """ 返回表示该数字的时占用的最少位数 """ 19 """ 20 int.bit_length() -> int 21 22 Number of bits necessary to represent self in binary. 23 >>> bin(37) 24 '0b100101' 25 >>> (37).bit_length() 26 """ 27 return 0 28 29 def conjugate(self, *args, **kwargs): # real signature unknown 30 """ 返回该复数的共轭复数 """ 31 """ Returns self, the complex conjugate of any int. """ 32 pass 33 34 def __abs__(self): 35 """ 返回绝对值 """ 36 """ x.__abs__() <==> abs(x) """ 37 pass 38 39 def __add__(self, y): 40 """ x.__add__(y) <==> x+y """ 41 pass 42 43 def __and__(self, y): 44 """ x.__and__(y) <==> x&y """ 45 pass 46 47 def __cmp__(self, y): 48 """ 比较两个数大小 """ 49 """ x.__cmp__(y) <==> cmp(x,y) """ 50 pass 51 52 def __coerce__(self, y): 53 """ 强制生成一个元组 """ 54 """ x.__coerce__(y) <==> coerce(x, y) """ 55 pass 56 57 def __divmod__(self, y): 58 """ 相除,得到商和余数组成的元组 """ 59 """ x.__divmod__(y) <==> divmod(x, y) """ 60 pass 61 62 def __div__(self, y): 63 """ x.__div__(y) <==> x/y """ 64 pass 65 66 def __float__(self): 67 """ 转换为浮点类型 """ 68 """ x.__float__() <==> float(x) """ 69 pass 70 71 def __floordiv__(self, y): 72 """ x.__floordiv__(y) <==> x//y """ 73 pass 74 75 def __format__(self, *args, **kwargs): # real signature unknown 76 pass 77 78 def __getattribute__(self, name): 79 """ x.__getattribute__('name') <==> x.name """ 80 pass 81 82 def __getnewargs__(self, *args, **kwargs): # real signature unknown 83 """ 内部调用 __new__方法或创建对象时传入参数使用 """ 84 pass 85 86 def __hash__(self): 87 """如果对象object为哈希表类型,返回对象object的哈希值。哈希值为整数。在字典查找中,哈希值用于快速比较字典的键。两个数值如果相等,则哈希值也相等。""" 88 """ x.__hash__() <==> hash(x) """ 89 pass 90 91 def __hex__(self): 92 """ 返回当前数的 十六进制 表示 """ 93 """ x.__hex__() <==> hex(x) """ 94 pass 95 96 def __index__(self): 97 """ 用于切片,数字无意义 """ 98 """ x[y:z] <==> x[y.__index__():z.__index__()] """ 99 pass 100 101 def __init__(self, x, base=10): # known special case of int.__init__ 102 """ 构造方法,执行 x = 123 或 x = int(10) 时,自动调用,暂时忽略 """ 103 """ 104 int(x=0) -> int or long 105 int(x, base=10) -> int or long 106 107 Convert a number or string to an integer, or return 0 if no arguments 108 are given. If x is floating point, the conversion truncates towards zero. 109 If x is outside the integer range, the function returns a long instead. 110 111 If x is not a number or if base is given, then x must be a string or 112 Unicode object representing an integer literal in the given base. The 113 literal can be preceded by '+' or '-' and be surrounded by whitespace. 114 The base defaults to 10. Valid bases are 0 and 2-36. Base 0 means to 115 interpret the base from the string as an integer literal. 116 >>> int('0b100', base=0) 117 # (copied from class doc) 118 """ 119 pass 120 121 def __int__(self): 122 """ 转换为整数 """ 123 """ x.__int__() <==> int(x) """ 124 pass 125 126 def __invert__(self): 127 """ x.__invert__() <==> ~x """ 128 pass 129 130 def __long__(self): 131 """ 转换为长整数 """ 132 """ x.__long__() <==> long(x) """ 133 pass 134 135 def __lshift__(self, y): 136 """ x.__lshift__(y) <==> x<<y """ 137 pass 138 139 def __mod__(self, y): 140 """ x.__mod__(y) <==> x%y """ 141 pass 142 143 def __mul__(self, y): 144 """ x.__mul__(y) <==> x*y """ 145 pass 146 147 def __neg__(self): 148 """ x.__neg__() <==> -x """ 149 pass 150 151 @staticmethod # known case of __new__ 152 def __new__(S, *more): 153 """ T.__new__(S, ...) -> a new object with type S, a subtype of T """ 154 pass 155 156 def __nonzero__(self): 157 """ x.__nonzero__() <==> x != 0 """ 158 pass 159 160 def __oct__(self): 161 """ 返回改值的 八进制 表示 """ 162 """ x.__oct__() <==> oct(x) """ 163 pass 164 165 def __or__(self, y): 166 """ x.__or__(y) <==> x|y """ 167 pass 168 169 def __pos__(self): 170 """ x.__pos__() <==> +x """ 171 pass 172 173 def __pow__(self, y, z=None): 174 """ 幂,次方 """ 175 """ x.__pow__(y[, z]) <==> pow(x, y[, z]) """ 176 pass 177 178 def __radd__(self, y): 179 """ x.__radd__(y) <==> y+x """ 180 pass 181 182 def __rand__(self, y): 183 """ x.__rand__(y) <==> y&x """ 184 pass 185 186 def __rdivmod__(self, y): 187 """ x.__rdivmod__(y) <==> divmod(y, x) """ 188 pass 189 190 def __rdiv__(self, y): 191 """ x.__rdiv__(y) <==> y/x """ 192 pass 193 194 def __repr__(self): 195 """转化为解释器可读取的形式 """ 196 """ x.__repr__() <==> repr(x) """ 197 pass 198 199 def __str__(self): 200 """转换为人阅读的形式,如果没有适于人阅读的解释形式的话,则返回解释器课阅读的形式""" 201 """ x.__str__() <==> str(x) """ 202 pass 203 204 def __rfloordiv__(self, y): 205 """ x.__rfloordiv__(y) <==> y//x """ 206 pass 207 208 def __rlshift__(self, y): 209 """ x.__rlshift__(y) <==> y<<x """ 210 pass 211 212 def __rmod__(self, y): 213 """ x.__rmod__(y) <==> y%x """ 214 pass 215 216 def __rmul__(self, y): 217 """ x.__rmul__(y) <==> y*x """ 218 pass 219 220 def __ror__(self, y): 221 """ x.__ror__(y) <==> y|x """ 222 pass 223 224 def __rpow__(self, x, z=None): 225 """ y.__rpow__(x[, z]) <==> pow(x, y[, z]) """ 226 pass 227 228 def __rrshift__(self, y): 229 """ x.__rrshift__(y) <==> y>>x """ 230 pass 231 232 def __rshift__(self, y): 233 """ x.__rshift__(y) <==> x>>y """ 234 pass 235 236 def __rsub__(self, y): 237 """ x.__rsub__(y) <==> y-x """ 238 pass 239 240 def __rtruediv__(self, y): 241 """ x.__rtruediv__(y) <==> y/x """ 242 pass 243 244 def __rxor__(self, y): 245 """ x.__rxor__(y) <==> y^x """ 246 pass 247 248 def __sub__(self, y): 249 """ x.__sub__(y) <==> x-y """ 250 pass 251 252 def __truediv__(self, y): 253 """ x.__truediv__(y) <==> x/y """ 254 pass 255 256 def __trunc__(self, *args, **kwargs): 257 """ 返回数值被截取为整形的值,在整形中无意义 """ 258 pass 259 260 def __xor__(self, y): 261 """ x.__xor__(y) <==> x^y """ 262 pass 263 264 denominator = property(lambda self: object(), lambda self, v: None, lambda self: None) # default 265 """ 分母 = 1 """ 266 """the denominator of a rational number in lowest terms""" 267 268 imag = property(lambda self: object(), lambda self, v: None, lambda self: None) # default 269 """ 虚数,无意义 """ 270 """the imaginary part of a complex number""" 271 272 numerator = property(lambda self: object(), lambda self, v: None, lambda self: None) # default 273 """ 分子 = 数字大小 """ 274 """the numerator of a rational number in lowest terms""" 275 276 real = property(lambda self: object(), lambda self, v: None, lambda self: None) # default 277 """ 实属,无意义 """ 278 """the real part of a complex number""" 279 280 int
二、长整型
可能如:2147483649、9223372036854775807
每个长整型都具备如下功能:
1 class long(object): 2 """ 3 long(x=0) -> long 4 long(x, base=10) -> long 5 6 Convert a number or string to a long integer, or return 0L if no arguments 7 are given. If x is floating point, the conversion truncates towards zero. 8 9 If x is not a number or if base is given, then x must be a string or 10 Unicode object representing an integer literal in the given base. The 11 literal can be preceded by '+' or '-' and be surrounded by whitespace. 12 The base defaults to 10. Valid bases are 0 and 2-36. Base 0 means to 13 interpret the base from the string as an integer literal. 14 >>> int('0b100', base=0) 15 4L 16 """ 17 def bit_length(self): # real signature unknown; restored from __doc__ 18 """ 19 long.bit_length() -> int or long 20 21 Number of bits necessary to represent self in binary. 22 >>> bin(37L) 23 '0b100101' 24 >>> (37L).bit_length() 25 """ 26 return 0 27 28 def conjugate(self, *args, **kwargs): # real signature unknown 29 """ Returns self, the complex conjugate of any long. """ 30 pass 31 32 def __abs__(self): # real signature unknown; restored from __doc__ 33 """ x.__abs__() <==> abs(x) """ 34 pass 35 36 def __add__(self, y): # real signature unknown; restored from __doc__ 37 """ x.__add__(y) <==> x+y """ 38 pass 39 40 def __and__(self, y): # real signature unknown; restored from __doc__ 41 """ x.__and__(y) <==> x&y """ 42 pass 43 44 def __cmp__(self, y): # real signature unknown; restored from __doc__ 45 """ x.__cmp__(y) <==> cmp(x,y) """ 46 pass 47 48 def __coerce__(self, y): # real signature unknown; restored from __doc__ 49 """ x.__coerce__(y) <==> coerce(x, y) """ 50 pass 51 52 def __divmod__(self, y): # real signature unknown; restored from __doc__ 53 """ x.__divmod__(y) <==> divmod(x, y) """ 54 pass 55 56 def __div__(self, y): # real signature unknown; restored from __doc__ 57 """ x.__div__(y) <==> x/y """ 58 pass 59 60 def __float__(self): # real signature unknown; restored from __doc__ 61 """ x.__float__() <==> float(x) """ 62 pass 63 64 def __floordiv__(self, y): # real signature unknown; restored from __doc__ 65 """ x.__floordiv__(y) <==> x//y """ 66 pass 67 68 def __format__(self, *args, **kwargs): # real signature unknown 69 pass 70 71 def __getattribute__(self, name): # real signature unknown; restored from __doc__ 72 """ x.__getattribute__('name') <==> x.name """ 73 pass 74 75 def __getnewargs__(self, *args, **kwargs): # real signature unknown 76 pass 77 78 def __hash__(self): # real signature unknown; restored from __doc__ 79 """ x.__hash__() <==> hash(x) """ 80 pass 81 82 def __hex__(self): # real signature unknown; restored from __doc__ 83 """ x.__hex__() <==> hex(x) """ 84 pass 85 86 def __index__(self): # real signature unknown; restored from __doc__ 87 """ x[y:z] <==> x[y.__index__():z.__index__()] """ 88 pass 89 90 def __init__(self, x=0): # real signature unknown; restored from __doc__ 91 pass 92 93 def __int__(self): # real signature unknown; restored from __doc__ 94 """ x.__int__() <==> int(x) """ 95 pass 96 97 def __invert__(self): # real signature unknown; restored from __doc__ 98 """ x.__invert__() <==> ~x """ 99 pass 100 101 def __long__(self): # real signature unknown; restored from __doc__ 102 """ x.__long__() <==> long(x) """ 103 pass 104 105 def __lshift__(self, y): # real signature unknown; restored from __doc__ 106 """ x.__lshift__(y) <==> x<<y """ 107 pass 108 109 def __mod__(self, y): # real signature unknown; restored from __doc__ 110 """ x.__mod__(y) <==> x%y """ 111 pass 112 113 def __mul__(self, y): # real signature unknown; restored from __doc__ 114 """ x.__mul__(y) <==> x*y """ 115 pass 116 117 def __neg__(self): # real signature unknown; restored from __doc__ 118 """ x.__neg__() <==> -x """ 119 pass 120 121 @staticmethod # known case of __new__ 122 def __new__(S, *more): # real signature unknown; restored from __doc__ 123 """ T.__new__(S, ...) -> a new object with type S, a subtype of T """ 124 pass 125 126 def __nonzero__(self): # real signature unknown; restored from __doc__ 127 """ x.__nonzero__() <==> x != 0 """ 128 pass 129 130 def __oct__(self): # real signature unknown; restored from __doc__ 131 """ x.__oct__() <==> oct(x) """ 132 pass 133 134 def __or__(self, y): # real signature unknown; restored from __doc__ 135 """ x.__or__(y) <==> x|y """ 136 pass 137 138 def __pos__(self): # real signature unknown; restored from __doc__ 139 """ x.__pos__() <==> +x """ 140 pass 141 142 def __pow__(self, y, z=None): # real signature unknown; restored from __doc__ 143 """ x.__pow__(y[, z]) <==> pow(x, y[, z]) """ 144 pass 145 146 def __radd__(self, y): # real signature unknown; restored from __doc__ 147 """ x.__radd__(y) <==> y+x """ 148 pass 149 150 def __rand__(self, y): # real signature unknown; restored from __doc__ 151 """ x.__rand__(y) <==> y&x """ 152 pass 153 154 def __rdivmod__(self, y): # real signature unknown; restored from __doc__ 155 """ x.__rdivmod__(y) <==> divmod(y, x) """ 156 pass 157 158 def __rdiv__(self, y): # real signature unknown; restored from __doc__ 159 """ x.__rdiv__(y) <==> y/x """ 160 pass 161 162 def __repr__(self): # real signature unknown; restored from __doc__ 163 """ x.__repr__() <==> repr(x) """ 164 pass 165 166 def __rfloordiv__(self, y): # real signature unknown; restored from __doc__ 167 """ x.__rfloordiv__(y) <==> y//x """ 168 pass 169 170 def __rlshift__(self, y): # real signature unknown; restored from __doc__ 171 """ x.__rlshift__(y) <==> y<<x """ 172 pass 173 174 def __rmod__(self, y): # real signature unknown; restored from __doc__ 175 """ x.__rmod__(y) <==> y%x """ 176 pass 177 178 def __rmul__(self, y): # real signature unknown; restored from __doc__ 179 """ x.__rmul__(y) <==> y*x """ 180 pass 181 182 def __ror__(self, y): # real signature unknown; restored from __doc__ 183 """ x.__ror__(y) <==> y|x """ 184 pass 185 186 def __rpow__(self, x, z=None): # real signature unknown; restored from __doc__ 187 """ y.__rpow__(x[, z]) <==> pow(x, y[, z]) """ 188 pass 189 190 def __rrshift__(self, y): # real signature unknown; restored from __doc__ 191 """ x.__rrshift__(y) <==> y>>x """ 192 pass 193 194 def __rshift__(self, y): # real signature unknown; restored from __doc__ 195 """ x.__rshift__(y) <==> x>>y """ 196 pass 197 198 def __rsub__(self, y): # real signature unknown; restored from __doc__ 199 """ x.__rsub__(y) <==> y-x """ 200 pass 201 202 def __rtruediv__(self, y): # real signature unknown; restored from __doc__ 203 """ x.__rtruediv__(y) <==> y/x """ 204 pass 205 206 def __rxor__(self, y): # real signature unknown; restored from __doc__ 207 """ x.__rxor__(y) <==> y^x """ 208 pass 209 210 def __sizeof__(self, *args, **kwargs): # real signature unknown 211 """ Returns size in memory, in bytes """ 212 pass 213 214 def __str__(self): # real signature unknown; restored from __doc__ 215 """ x.__str__() <==> str(x) """ 216 pass 217 218 def __sub__(self, y): # real signature unknown; restored from __doc__ 219 """ x.__sub__(y) <==> x-y """ 220 pass 221 222 def __truediv__(self, y): # real signature unknown; restored from __doc__ 223 """ x.__truediv__(y) <==> x/y """ 224 pass 225 226 def __trunc__(self, *args, **kwargs): # real signature unknown 227 """ Truncating an Integral returns itself. """ 228 pass 229 230 def __xor__(self, y): # real signature unknown; restored from __doc__ 231 """ x.__xor__(y) <==> x^y """ 232 pass 233 234 denominator = property(lambda self: object(), lambda self, v: None, lambda self: None) # default 235 """the denominator of a rational number in lowest terms""" 236 237 imag = property(lambda self: object(), lambda self, v: None, lambda self: None) # default 238 """the imaginary part of a complex number""" 239 240 numerator = property(lambda self: object(), lambda self, v: None, lambda self: None) # default 241 """the numerator of a rational number in lowest terms""" 242 243 real = property(lambda self: object(), lambda self, v: None, lambda self: None) # default 244 """the real part of a complex number""" 245 246 long
三、浮点型
如:3.14、2.88
每个浮点型都具备如下功能
1 class float(object): 2 """ 3 float(x) -> floating point number 4 5 Convert a string or number to a floating point number, if possible. 6 """ 7 def as_integer_ratio(self): 8 """ 获取改值的最简比 """ 9 """ 10 float.as_integer_ratio() -> (int, int) 11 12 Return a pair of integers, whose ratio is exactly equal to the original 13 float and with a positive denominator. 14 Raise OverflowError on infinities and a ValueError on NaNs. 15 16 >>> (10.0).as_integer_ratio() 17 (10, 1) 18 >>> (0.0).as_integer_ratio() 19 (0, 1) 20 >>> (-.25).as_integer_ratio() 21 (-1, 4) 22 """ 23 pass 24 25 def conjugate(self, *args, **kwargs): # real signature unknown 26 """ Return self, the complex conjugate of any float. """ 27 pass 28 29 def fromhex(self, string): 30 """ 将十六进制字符串转换成浮点型 """ 31 """ 32 float.fromhex(string) -> float 33 34 Create a floating-point number from a hexadecimal string. 35 >>> float.fromhex('0x1.ffffp10') 36 2047.984375 37 >>> float.fromhex('-0x1p-1074') 38 -4.9406564584124654e-324 39 """ 40 return 0.0 41 42 def hex(self): 43 """ 返回当前值的 16 进制表示 """ 44 """ 45 float.hex() -> string 46 47 Return a hexadecimal representation of a floating-point number. 48 >>> (-0.1).hex() 49 '-0x1.999999999999ap-4' 50 >>> 3.14159.hex() 51 '0x1.921f9f01b866ep+1' 52 """ 53 return "" 54 55 def is_integer(self, *args, **kwargs): # real signature unknown 56 """ Return True if the float is an integer. """ 57 pass 58 59 def __abs__(self): 60 """ x.__abs__() <==> abs(x) """ 61 pass 62 63 def __add__(self, y): 64 """ x.__add__(y) <==> x+y """ 65 pass 66 67 def __coerce__(self, y): 68 """ x.__coerce__(y) <==> coerce(x, y) """ 69 pass 70 71 def __divmod__(self, y): 72 """ x.__divmod__(y) <==> divmod(x, y) """ 73 pass 74 75 def __div__(self, y): 76 """ x.__div__(y) <==> x/y """ 77 pass 78 79 def __eq__(self, y): 80 """ x.__eq__(y) <==> x==y """ 81 pass 82 83 def __float__(self): 84 """ x.__float__() <==> float(x) """ 85 pass 86 87 def __floordiv__(self, y): 88 """ x.__floordiv__(y) <==> x//y """ 89 pass 90 91 def __format__(self, format_spec): 92 """ 93 float.__format__(format_spec) -> string 94 95 Formats the float according to format_spec. 96 """ 97 return "" 98 99 def __getattribute__(self, name): 100 """ x.__getattribute__('name') <==> x.name """ 101 pass 102 103 def __getformat__(self, typestr): 104 """ 105 float.__getformat__(typestr) -> string 106 107 You probably don't want to use this function. It exists mainly to be 108 used in Python's test suite. 109 110 typestr must be 'double' or 'float'. This function returns whichever of 111 'unknown', 'IEEE, big-endian' or 'IEEE, little-endian' best describes the 112 format of floating point numbers used by the C type named by typestr. 113 """ 114 return "" 115 116 def __getnewargs__(self, *args, **kwargs): # real signature unknown 117 pass 118 119 def __ge__(self, y): 120 """ x.__ge__(y) <==> x>=y """ 121 pass 122 123 def __gt__(self, y): 124 """ x.__gt__(y) <==> x>y """ 125 pass 126 127 def __hash__(self): 128 """ x.__hash__() <==> hash(x) """ 129 pass 130 131 def __init__(self, x): 132 pass 133 134 def __int__(self): 135 """ x.__int__() <==> int(x) """ 136 pass 137 138 def __le__(self, y): 139 """ x.__le__(y) <==> x<=y """ 140 pass 141 142 def __long__(self): 143 """ x.__long__() <==> long(x) """ 144 pass 145 146 def __lt__(self, y): 147 """ x.__lt__(y) <==> x<y """ 148 pass 149 150 def __mod__(self, y): 151 """ x.__mod__(y) <==> x%y """ 152 pass 153 154 def __mul__(self, y): 155 """ x.__mul__(y) <==> x*y """ 156 pass 157 158 def __neg__(self): 159 """ x.__neg__() <==> -x """ 160 pass 161 162 @staticmethod # known case of __new__ 163 def __new__(S, *more): 164 """ T.__new__(S, ...) -> a new object with type S, a subtype of T """ 165 pass 166 167 def __ne__(self, y): 168 """ x.__ne__(y) <==> x!=y """ 169 pass 170 171 def __nonzero__(self): 172 """ x.__nonzero__() <==> x != 0 """ 173 pass 174 175 def __pos__(self): 176 """ x.__pos__() <==> +x """ 177 pass 178 179 def __pow__(self, y, z=None): 180 """ x.__pow__(y[, z]) <==> pow(x, y[, z]) """ 181 pass 182 183 def __radd__(self, y): 184 """ x.__radd__(y) <==> y+x """ 185 pass 186 187 def __rdivmod__(self, y): 188 """ x.__rdivmod__(y) <==> divmod(y, x) """ 189 pass 190 191 def __rdiv__(self, y): 192 """ x.__rdiv__(y) <==> y/x """ 193 pass 194 195 def __repr__(self): 196 """ x.__repr__() <==> repr(x) """ 197 pass 198 199 def __rfloordiv__(self, y): 200 """ x.__rfloordiv__(y) <==> y//x """ 201 pass 202 203 def __rmod__(self, y): 204 """ x.__rmod__(y) <==> y%x """ 205 pass 206 207 def __rmul__(self, y): 208 """ x.__rmul__(y) <==> y*x """ 209 pass 210 211 def __rpow__(self, x, z=None): 212 """ y.__rpow__(x[, z]) <==> pow(x, y[, z]) """ 213 pass 214 215 def __rsub__(self, y): 216 """ x.__rsub__(y) <==> y-x """ 217 pass 218 219 def __rtruediv__(self, y): 220 """ x.__rtruediv__(y) <==> y/x """ 221 pass 222 223 def __setformat__(self, typestr, fmt): 224 """ 225 float.__setformat__(typestr, fmt) -> None 226 227 You probably don't want to use this function. It exists mainly to be 228 used in Python's test suite. 229 230 typestr must be 'double' or 'float'. fmt must be one of 'unknown', 231 'IEEE, big-endian' or 'IEEE, little-endian', and in addition can only be 232 one of the latter two if it appears to match the underlying C reality. 233 234 Override the automatic determination of C-level floating point type. 235 This affects how floats are converted to and from binary strings. 236 """ 237 pass 238 239 def __str__(self): 240 """ x.__str__() <==> str(x) """ 241 pass 242 243 def __sub__(self, y): 244 """ x.__sub__(y) <==> x-y """ 245 pass 246 247 def __truediv__(self, y): 248 """ x.__truediv__(y) <==> x/y """ 249 pass 250 251 def __trunc__(self, *args, **kwargs): # real signature unknown 252 """ Return the Integral closest to x between 0 and x. """ 253 pass 254 255 imag = property(lambda self: object(), lambda self, v: None, lambda self: None) # default 256 """the imaginary part of a complex number""" 257 258 real = property(lambda self: object(), lambda self, v: None, lambda self: None) # default 259 """the real part of a complex number""" 260 261 float
四、字符串
如:'wupeiqi'、'alex'
每个字符串都具备如下功能:
字符串:
任何语言基本上都是 字符串和集合的操作
Python 的集合 有基本的字典 列表 元祖
学任何语言 建议先学 语言的 字符串 和集合
下面看一下字符串的操作
1 字符串str 2 name = 'eric' 3 #type返回对象的类 4 5 print(type(name)) 6 #dir返回了类里边都有哪些方法(成员) 7 #dir 获取类的所有成员 8 print(dir(name))
#包含
1 def __contains__(self, *args, **kwargs): # real signature unknown 2 """ Return key in self. """ 3 name = 'eric' 4 #查看name里边是否包含了er,如果包含了则返回True,否则返回Fales 5 #name.__contains__('er') 等同于 bb = 'er6' in name 6 aa = name.__contains__('er') 7 bb = 'er6' in name 8 print(aa) 9 print(bb) 10 返回结果: 11 True 12 False
1 #字符串的相等,上面讲过了 2 def __eq__(self, *args, **kwargs): # real signature unknown 3 """ Return self==value. """ 4 pass
1 #反射的时候会用到 __getattribute__ 2 def __getattribute__(self, *args, **kwargs): # real signature unknown 3 """ Return getattr(self, name). """ 4 pass
1 #进行首字母大写 2 def capitalize(self): # real signature unknown; restored from __doc__ 3 """ 4 S.capitalize() -> str 5 6 Return a capitalized version of S, i.e. make the first character 7 have upper case and the rest lower case. 8 """ 9 name = 'eric' 10 ww = name.capitalize() 11 print(ww) 12 13 打印: 14 Eric
1 def center(self, width, fillchar=None): # real signature unknown; restored from __doc__ 2 """ 3 S.center(width[, fillchar]) -> str 4 5 Return S centered in a string of length width. Padding is 6 done using the specified fill character (default is a space) 7 """ 8 #center输居中的意思, ww.center(20,'-')打印20是个"-" 9 name = 'eric' 10 print(name.center(20,'-')) 11 输出: 12 --------eric--------
#count 是计算子系列出现的次数
1 def count(self, sub, start=None, end=None): # real signature unknown; restored from __doc__ 2 """ 3 S.count(sub[, start[, end]]) -> int 4 5 Return the number of non-overlapping occurrences of substring sub in 6 string S[start:end]. Optional arguments start and end are 7 interpreted as in slice notation. 8 """ 9 name = 'ericaaasfasfasfadfasdfasdf' 10 11 print(name.count('e')) 12 print(name.count('a') 13 14 打印 15 1 16 8 17 #上列可以看出 e 出现了1次,a 出现了 8次 18 #下面 制定变量的 区间内的字符出现的次数 19 name = 'ericaaasfasfasfadfasdfasdf' 20 print(name.count('a',5,10)) 21 打印 22 3 23 #表示在name变量中字符串的第三个位置到第十个位置a出现了多少次
1 def encode(self, encoding='utf-8', errors='strict'): # real signature unknown; restored from __doc__ 2 """ 3 S.encode(encoding='utf-8', errors='strict') -> bytes 4 5 Encode S using the codec registered for encoding. Default encoding 6 is 'utf-8'. errors may be given to set a different error 7 handling scheme. Default is 'strict' meaning that encoding errors raise 8 a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and 9 'xmlcharrefreplace' as well as any other name registered with 10 codecs.register_error that can handle UnicodeEncodeErrors. 11 """ 12 name = '二傻' 13 result = name.encode('gbk') 14 print(result) 15 打印 16 b'\xb6\xfe\xc9\xb5' 17 #在 python 2.X 里 如果是UTF-8 要先转成unicode在转成GBK 18 #3.x里如果是UTF-8 要转成GBK的话,就可以直接通过encoede 转成GBK 19 #所以在3.x里就不用自己动手转成unicode了,3.x里Python自己就给转了
1 #看看这个字符串是不是以某个字符或者某个子系列结尾的 2 def endswith(self, suffix, start=None, end=None): # real signature unknown; restored from __doc__ 3 """ 4 S.endswith(suffix[, start[, end]]) -> bool 5 6 Return True if S ends with the specified suffix, False otherwise. 7 With optional start, test S beginning at that position. 8 With optional end, stop comparing S at that position. 9 suffix can also be a tuple of strings to try. 10 """ 11 name = 'asdf' 12 print(name.endswith('f')) 13 打印 14 True 15 name = 'asdf' 16 print(name.endswith('w)) 17 打印 18 False 19 #制定字符串区间判断是否为某个字符或者某个子系列结尾的 20 #注意: 正常来说是从0位起始位置,asdf 判断0=a,1=s,2=d,3=f,这里为什么写成0,3,因为是遵循大于等于0小于3 21 name = 'asdf' 22 print(name.endswith('d',0,3)) 23 打印 24 True
#用来转换,将\t转换成空格,默认情况下一个\t 转换成 8个空格
1 def expandtabs(self, tabsize=8): # real signature unknown; restored from __doc__ 2 """ 3 S.expandtabs(tabsize=8) -> str 4 5 Return a copy of S where all tab characters are expanded using spaces. 6 If tabsize is not given, a tab size of 8 characters is assumed. 7 """ 8 return "" 9 name = 'a\tsdf' 10 print(name.expandtabs()) 11 打印 12 a sdf 13 ------------------------------ 14 name = 'a\tsdf' 15 print(name.expandtabs(20)) 16 打印 17 a sdf
#在一个字符串里找某个子序列,找到之后返回这个子序列在这个字符串的索引位置
1 def find(self, sub, start=None, end=None): # real signature unknown; restored from __doc__ 2 """ 3 S.find(sub[, start[, end]]) -> int 4 5 Return the lowest index in S where substring sub is found, 6 such that sub is contained within S[start:end]. Optional 7 arguments start and end are interpreted as in slice notation. 8 9 Return -1 on failure. 10 """ 11 12 13 name = 'asdf' 14 print(name.find('s')) 15 打印 16 1 17 18 name = 'asdf' 19 print(name.find('f')) 20 打印 21 3 22 23 #如果找一个不存在的返回-1 24 name = 'asdf' 25 print(name.find('w')) 26 打印 27 -1 28 29 30 #制定区间查找,这里会打印第一个查找到的索引位置 31 name = 'asdfsdfsafasdf' 32 print(name.find('a',3,9)) 33 打印 34 8 35 36 37 #注意:find 没有找到会返回-1,index 没有找到的话会报报错 38 name = 'asdfsdfsafasdf' 39 print(name.index('s',3,9))
#format用来做字符串格式化的,等同于 %s
1 def format(*args, **kwargs): # known special case of str.format 2 """ 3 S.format(*args, **kwargs) -> str 4 5 Return a formatted version of S, using substitutions from args and kwargs. 6 The substitutions are identified by braces ('{' and '}'). 7 """ 8 pass 9 10 ---------------------------------- 11 name = 'asdfsdfsafasdf {0} as {1} ww {2}' 12 result = name.format('wwwwww','eeeee','rrrr') 13 print(result) 14 打印 15 asdfsdfsafasdf wwwwww as eeeee ww rrrr 16 17 ----------------------------------- 18 19 name = 'asdfsdfsafasdf {name} as {pa} ww {grou}' 20 result = name.format(name = 'wwwwww',pa = 'eeeee',grou = 'rrrr') 21 print(result) 22 打印 23 asdfsdfsafasdf wwwwww as eeeee ww rrrr
#判断是否是字母或者是数字
1 def isalnum(self): # real signature unknown; restored from __doc__ 2 """ 3 S.isalnum() -> bool 4 5 Return True if all characters in S are alphanumeric 6 and there is at least one character in S, False otherwise. 7 """
#是否是字母
1 def isalpha(self): # real signature unknown; restored from __doc__ 2 """ 3 S.isalpha() -> bool 4 5 Return True if all characters in S are alphabetic 6 and there is at least one character in S, False otherwise. 7 """
#是否是十进制小数
1 def isdecimal(self): # real signature unknown; restored from __doc__ 2 """ 3 S.isdecimal() -> bool 4 5 Return True if there are only decimal characters in S, 6 False otherwise. 7 """ 8 return False
#是否是数字
1 def isdigit(self): # real signature unknown; restored from __doc__ 2 """ 3 S.isdigit() -> bool 4 5 Return True if all characters in S are digits 6 and there is at least one character in S, False otherwise. 7 """ 8 return False
#是否全部是小写
1 def islower(self): # real signature unknown; restored from __doc__ 2 """ 3 S.islower() -> bool 4 5 Return True if all cased characters in S are lowercase and there is 6 at least one cased character in S, False otherwise. 7 """ 8 return False
#是否是数字
1 def isnumeric(self): # real signature unknown; restored from __doc__ 2 """ 3 S.isnumeric() -> bool 4 5 Return True if there are only numeric characters in S, 6 False otherwise. 7 """ 8 return False
#是否可以打印
1 def isprintable(self): # real signature unknown; restored from __doc__ 2 """ 3 S.isprintable() -> bool 4 5 Return True if all characters in S are considered 6 printable in repr() or S is empty, False otherwise. 7 """ 8 return False
#是否是空格
1 def isspace(self): # real signature unknown; restored from __doc__ 2 """ 3 S.isspace() -> bool 4 5 Return True if all characters in S are whitespace 6 and there is at least one character in S, False otherwise. 7 """ 8 return False
#是否是标题,给你个字符串是否是标题,标题首字母都是大写,判断首字母是否是大写
1 def istitle(self): # real signature unknown; restored from __doc__ 2 """ 3 S.istitle() -> bool 4 5 Return True if S is a titlecased string and there is at least one 6 character in S, i.e. upper- and titlecase characters may only 7 follow uncased characters and lowercase characters only cased ones. 8 Return False otherwise. 9 """ 10 return False
#是否全部是大写
1 def isupper(self): # real signature unknown; restored from __doc__ 2 """ 3 S.isupper() -> bool 4 5 Return True if all cased characters in S are uppercase and there is 6 at least one cased character in S, False otherwise. 7 """ 8 return False
#用来做拼接
1 def join(self, iterable): # real signature unknown; restored from __doc__ 2 """ 3 S.join(iterable) -> str 4 5 Return a string which is the concatenation of the strings in the 6 iterable. The separator between elements is S. 7 """ 8 aa = ['1','2','3','a','b','b'] 9 result = "".join(aa) 10 print(result) 11 打印 12 123abb 13 ----------------------------- 14 aa = ['1','2','3','a','b','c'] 15 result = "_".join(aa) 16 print(result) 17 打印 18 1_2_3_a_b_c
#于center 一样的 center 放在中间,ljust 放在左边
1 def ljust(self, width, fillchar=None): # real signature unknown; restored from __doc__ 2 """ 3 S.ljust(width[, fillchar]) -> str 4 5 Return S left-justified in a Unicode string of length width. Padding is 6 done using the specified fill character (default is a space). 7 """
#于center 一样的 center 放在中间,ljust 放在右边
1 def rjust(self, width, fillchar=None): # real signature unknown; restored from __doc__ 2 """ 3 S.rjust(width[, fillchar]) -> str 4 5 Return S right-justified in a string of length width. Padding is 6 done using the specified fill character (default is a space). 7 """
#拿到一个字母之后变成小写
1 def lower(self): # real signature unknown; restored from __doc__ 2 """ 3 S.lower() -> str 4 5 Return a copy of the string S converted to lowercase. 6 """
#strip 是两边空格全去除,lstrip是去除右边的
1 def lstrip(self, chars=None): # real signature unknown; restored from __doc__ 2 """ 3 S.lstrip([chars]) -> str 4 5 Return a copy of the string S with leading whitespace removed. 6 If chars is given and not None, remove characters in chars instead. 7 """ 8 return ""
#用来做分割
1 def partition(self, sep): # real signature unknown; restored from __doc__ 2 """ 3 S.partition(sep) -> (head, sep, tail) 4 5 Search for the separator sep in S, and return the part before it, 6 the separator itself, and the part after it. If the separator is not 7 found, return S and two empty strings. 8 """ 9 ww = 'asdfasdf' 10 ee = ww.partition('fa') 11 print(ee) 12 打印 13 ('asd', 'fa', 'sdf')
#替换
1 def replace(self, old, new, count=None): # real signature unknown; restored from __doc__ 2 """ 3 S.replace(old, new[, count]) -> str 4 5 Return a copy of S with all occurrences of substring 6 old replaced by new. If the optional argument count is 7 given, only the first count occurrences are replaced. 8 """ 9 ww = 'asdfasdfa' 10 #把fa 替换成123 11 ee = ww.replace('fa','123') 12 #把所有的a 替换成W 13 rr = ww.replace('a','W') 14 #只替换一个 15 tt = ww.replace('a','W',1) 16 print(ee) 17 print(rr) 18 print(tt) 19 打印 20 asd123sd123 21 WsdfWsdfW 22 Wsdfasdfa
#原来的find 是从左到右 找 rfind 是从右到左 找
1 def rfind(self, sub, start=None, end=None): # real signature unknown; restored from __doc__ 2 """ 3 S.rfind(sub[, start[, end]]) -> int 4 5 Return the highest index in S where substring sub is found, 6 such that sub is contained within S[start:end]. Optional 7 arguments start and end are interpreted as in slice notation. 8 9 Return -1 on failure. 10 """
#原来的index是从左到右找,rindex 是从右向左找
1 def rindex(self, sub, start=None, end=None): # real signature unknown; restored from __doc__ 2 """ 3 S.rindex(sub[, start[, end]]) -> int 4 5 Like S.rfind() but raise ValueError when the substring is not found. 6 """ 7 return 0
#也是 于上面提到的just 相反从右向左
1 def rjust(self, width, fillchar=None): # real signature unknown; restored from __doc__ 2 """ 3 S.rjust(width[, fillchar]) -> str 4 5 Return S right-justified in a string of length width. Padding is 6 done using the specified fill character (default is a space). 7 """
#于上面的意思一样 只是从右向左
1 def rpartition(self, sep): # real signature unknown; restored from __doc__ 2 """ 3 S.rpartition(sep) -> (head, sep, tail) 4 5 Search for the separator sep in S, starting at the end of S, and return 6 the part before it, the separator itself, and the part after it. If the 7 separator is not found, return two empty strings and S. 8 """
#split 是制定字符分割字符串, rsplit 是 从右向左制定字符串进行分割
1 def rsplit(self, sep=None, maxsplit=-1): # real signature unknown; restored from __doc__ 2 """ 3 S.rsplit(sep=None, maxsplit=-1) -> list of strings 4 5 Return a list of the words in S, using sep as the 6 delimiter string, starting at the end of the string and 7 working to the front. If maxsplit is given, at most maxsplit 8 splits are done. If sep is not specified, any whitespace string 9 is a separator. 10 """
#于strip 相反 从右向左
1 def rstrip(self, chars=None): # real signature unknown; restored from __doc__ 2 """ 3 S.rstrip([chars]) -> str 4 5 Return a copy of the string S with trailing whitespace removed. 6 If chars is given and not None, remove characters in chars instead. 7 """
#分割
1 def split(self, sep=None, maxsplit=-1): # real signature unknown; restored from __doc__ 2 """ 3 S.split(sep=None, maxsplit=-1) -> list of strings 4 5 Return a list of the words in S, using sep as the 6 delimiter string. If maxsplit is given, at most maxsplit 7 splits are done. If sep is not specified or is None, any 8 whitespace string is a separator and empty strings are 9 removed from the result. 10 """
#是根据换行符分割
1 def splitlines(self, keepends=None): # real signature unknown; restored from __doc__ 2 """ 3 S.splitlines([keepends]) -> list of strings 4 5 Return a list of the lines in S, breaking at line boundaries. 6 Line breaks are not included in the resulting list unless keepends 7 is given and true. 8 """
#以XXX开头
1 def startswith(self, prefix, start=None, end=None): # real signature unknown; restored from __doc__ 2 """ 3 S.startswith(prefix[, start[, end]]) -> bool 4 5 Return True if S starts with the specified prefix, False otherwise. 6 With optional start, test S beginning at that position. 7 With optional end, stop comparing S at that position. 8 prefix can also be a tuple of strings to try. 9 """
#大小写转换,把一个字符串的小写转换成大写,吧一个字符串的大写转换成想小写
1 def swapcase(self): # real signature unknown; restored from __doc__ 2 """ 3 S.swapcase() -> str 4 5 Return a copy of S with uppercase characters converted to lowercase 6 and vice versa. 7 """
#把所有字符串的首个字母变成大写
1 def title(self): # real signature unknown; restored from __doc__ 2 """ 3 S.title() -> str 4 5 Return a titlecased version of S, i.e. words start with title case 6 characters, all remaining cased characters have lower case. 7 """
#大写
1 def upper(self): # real signature unknown; restored from __doc__ 2 """ 3 S.upper() -> str 4 5 Return a copy of S converted to uppercase. 6 """
五、列表
如:[11,22,33]、['wupeiqi', 'alex']
每个列表都具备如下功能:
1 class list(object): 2 """ 3 list() -> new empty list 4 list(iterable) -> new list initialized from iterable's items 5 """ 6 def append(self, p_object): # real signature unknown; restored from __doc__ 7 """ L.append(object) -- append object to end """ 8 pass 9 10 def count(self, value): # real signature unknown; restored from __doc__ 11 """ L.count(value) -> integer -- return number of occurrences of value """ 12 return 0 13 14 def extend(self, iterable): # real signature unknown; restored from __doc__ 15 """ L.extend(iterable) -- extend list by appending elements from the iterable """ 16 pass 17 18 def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__ 19 """ 20 L.index(value, [start, [stop]]) -> integer -- return first index of value. 21 Raises ValueError if the value is not present. 22 """ 23 return 0 24 25 def insert(self, index, p_object): # real signature unknown; restored from __doc__ 26 """ L.insert(index, object) -- insert object before index """ 27 pass 28 29 def pop(self, index=None): # real signature unknown; restored from __doc__ 30 """ 31 L.pop([index]) -> item -- remove and return item at index (default last). 32 Raises IndexError if list is empty or index is out of range. 33 """ 34 pass 35 36 def remove(self, value): # real signature unknown; restored from __doc__ 37 """ 38 L.remove(value) -- remove first occurrence of value. 39 Raises ValueError if the value is not present. 40 """ 41 pass 42 43 def reverse(self): # real signature unknown; restored from __doc__ 44 """ L.reverse() -- reverse *IN PLACE* """ 45 pass 46 47 def sort(self, cmp=None, key=None, reverse=False): # real signature unknown; restored from __doc__ 48 """ 49 L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*; 50 cmp(x, y) -> -1, 0, 1 51 """ 52 pass 53 54 def __add__(self, y): # real signature unknown; restored from __doc__ 55 """ x.__add__(y) <==> x+y """ 56 pass 57 58 def __contains__(self, y): # real signature unknown; restored from __doc__ 59 """ x.__contains__(y) <==> y in x """ 60 pass 61 62 def __delitem__(self, y): # real signature unknown; restored from __doc__ 63 """ x.__delitem__(y) <==> del x[y] """ 64 pass 65 66 def __delslice__(self, i, j): # real signature unknown; restored from __doc__ 67 """ 68 x.__delslice__(i, j) <==> del x[i:j] 69 70 Use of negative indices is not supported. 71 """ 72 pass 73 74 def __eq__(self, y): # real signature unknown; restored from __doc__ 75 """ x.__eq__(y) <==> x==y """ 76 pass 77 78 def __getattribute__(self, name): # real signature unknown; restored from __doc__ 79 """ x.__getattribute__('name') <==> x.name """ 80 pass 81 82 def __getitem__(self, y): # real signature unknown; restored from __doc__ 83 """ x.__getitem__(y) <==> x[y] """ 84 pass 85 86 def __getslice__(self, i, j): # real signature unknown; restored from __doc__ 87 """ 88 x.__getslice__(i, j) <==> x[i:j] 89 90 Use of negative indices is not supported. 91 """ 92 pass 93 94 def __ge__(self, y): # real signature unknown; restored from __doc__ 95 """ x.__ge__(y) <==> x>=y """ 96 pass 97 98 def __gt__(self, y): # real signature unknown; restored from __doc__ 99 """ x.__gt__(y) <==> x>y """ 100 pass 101 102 def __iadd__(self, y): # real signature unknown; restored from __doc__ 103 """ x.__iadd__(y) <==> x+=y """ 104 pass 105 106 def __imul__(self, y): # real signature unknown; restored from __doc__ 107 """ x.__imul__(y) <==> x*=y """ 108 pass 109 110 def __init__(self, seq=()): # known special case of list.__init__ 111 """ 112 list() -> new empty list 113 list(iterable) -> new list initialized from iterable's items 114 # (copied from class doc) 115 """ 116 pass 117 118 def __iter__(self): # real signature unknown; restored from __doc__ 119 """ x.__iter__() <==> iter(x) """ 120 pass 121 122 def __len__(self): # real signature unknown; restored from __doc__ 123 """ x.__len__() <==> len(x) """ 124 pass 125 126 def __le__(self, y): # real signature unknown; restored from __doc__ 127 """ x.__le__(y) <==> x<=y """ 128 pass 129 130 def __lt__(self, y): # real signature unknown; restored from __doc__ 131 """ x.__lt__(y) <==> x<y """ 132 pass 133 134 def __mul__(self, n): # real signature unknown; restored from __doc__ 135 """ x.__mul__(n) <==> x*n """ 136 pass 137 138 @staticmethod # known case of __new__ 139 def __new__(S, *more): # real signature unknown; restored from __doc__ 140 """ T.__new__(S, ...) -> a new object with type S, a subtype of T """ 141 pass 142 143 def __ne__(self, y): # real signature unknown; restored from __doc__ 144 """ x.__ne__(y) <==> x!=y """ 145 pass 146 147 def __repr__(self): # real signature unknown; restored from __doc__ 148 """ x.__repr__() <==> repr(x) """ 149 pass 150 151 def __reversed__(self): # real signature unknown; restored from __doc__ 152 """ L.__reversed__() -- return a reverse iterator over the list """ 153 pass 154 155 def __rmul__(self, n): # real signature unknown; restored from __doc__ 156 """ x.__rmul__(n) <==> n*x """ 157 pass 158 159 def __setitem__(self, i, y): # real signature unknown; restored from __doc__ 160 """ x.__setitem__(i, y) <==> x[i]=y """ 161 pass 162 163 def __setslice__(self, i, j, y): # real signature unknown; restored from __doc__ 164 """ 165 x.__setslice__(i, j, y) <==> x[i:j]=y 166 167 Use of negative indices is not supported. 168 """ 169 pass 170 171 def __sizeof__(self): # real signature unknown; restored from __doc__ 172 """ L.__sizeof__() -- size of L in memory, in bytes """ 173 pass 174 175 __hash__ = None 176 177 list
#在列表的尾部在添加以个元素
1 def append(self, p_object): # real signature unknown; restored from __doc__ 2 """ L.append(object) -> None -- append object to end """ 3 pass 4 5 li = ['asx','11',2,3] 6 print(li) 7 li.append(55) 8 print(li) 9 打印 10 ['asx', '11', 2, 3] 11 ['asx', '11', 2, 3, 55]
#把列表清空
1 def clear(self): # real signature unknown; restored from __doc__ 2 """ L.clear() -> None -- remove all items from L """ 3 pass 4 5 li = ['asx','11',2,3] 6 print(li) 7 li.append(55) 8 print(li) 9 li.clear() 10 print(li) 11 打印 12 ['asx', '11', 2, 3] 13 ['asx', '11', 2, 3, 55] 14 []
#判断某个元素出现的次数
1 def count(self, value): # real signature unknown; restored from __doc__ 2 """ L.count(value) -> integer -- return number of occurrences of value """ 3 return 0 4 5 li = ['asx','asx','11',2,3] 6 ee = li.count('asx') 7 print(ee) 8 打印 9 2
#合并两个列表,也可以把一个列表和一个元祖合并
1 def extend(self, iterable): # real signature unknown; restored from __doc__ 2 """ L.extend(iterable) -> None -- extend list by appending elements from the iterable """ 3 pass 4 5 6 #对原来列表的扩展 7 li = list([1,2,3]) 8 print(li) 9 li.extend(['aa','bb']) 10 print(li) 11 打印 12 [1, 2, 3] 13 [1, 2, 3, 'aa', 'bb']
#获取下标
1 def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__ 2 """ 3 L.index(value, [start, [stop]]) -> integer -- return first index of value. 4 Raises ValueError if the value is not present. 5 """ 6 import sys 7 li = list([1,2,3]) 8 print(li) 9 li.extend(('aa','bb',)) 10 print(li) 11 ww = li.index('aa') 12 print(ww) 13 打印 14 [1, 2, 3] 15 [1, 2, 3, 'aa', 'bb'] 16 3
#指定下标 进行插入
1 def insert(self, index, p_object): # real signature unknown; restored from __doc__ 2 """ L.insert(index, object) -- insert object before index """ 3 pass 4 5 li = list([1,2,3]) 6 print(li) 7 li.extend(('aa','bb',)) 8 print(li) 9 ww = li.index(1) 10 li.insert(ww,'asd') 11 print(li) 12 打印 13 [1, 2, 3] 14 [1, 2, 3, 'aa', 'bb'] 15 ['asd', 1, 2, 3, 'aa', 'bb'] 16 ---------------------------------- 17 li = list([1,2,3]) 18 print(li) 19 li.insert(0,'asd') 20 print(li) 21 打印 22 [1, 2, 3] 23 ['asd', 1, 2, 3]
#移除某一项重新赋值
1 def pop(self, index=None): # real signature unknown; restored from __doc__ 2 """ 3 L.pop([index]) -> item -- remove and return item at index (default last). 4 Raises IndexError if list is empty or index is out of range. 5 """ 6 pass 7 8 li = list([1,2,3]) 9 print(li) 10 li.insert(0,'asd') 11 print(li) 12 #指定下标 13 ww = li.pop(0) 14 print(li) 15 print(ww) 16 打印 17 [1, 2, 3] 18 ['asd', 1, 2, 3] 19 [1, 2, 3] 20 asd
#删除,第一个
1 def remove(self, value): # real signature unknown; restored from __doc__ 2 """ 3 L.remove(value) -> None -- remove first occurrence of value. 4 Raises ValueError if the value is not present. 5 """ 6 pass 7 8 li = list(['asx','asx',2,3]) 9 li.remove('asx') 10 print(li) 11 12 打印 13 ['asx', 2, 3]
#反转
1 def reverse(self): # real signature unknown; restored from __doc__ 2 """ L.reverse() -- reverse *IN PLACE* """ 3 pass 4 5 li = list(['asx','11',2,3]) 6 print(li) 7 li.reverse() 8 print(li) 9 打印 10 ['asx', '11', 2, 3] 11 [3, 2, '11', 'asx']
#排序
1 def sort(self, key=None, reverse=False): # real signature unknown; restored from __doc__ 2 """ L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE* """ 3 pass 4
六、元组
如:(11,22,33)、('wupeiqi', 'alex')
每个元组都具备如下功能:
1 class tuple(object): 2 """ 3 tuple() -> empty tuple 4 tuple(iterable) -> tuple initialized from iterable's items 5 6 If the argument is a tuple, the return value is the same object. 7 """ 8 def count(self, value): # real signature unknown; restored from __doc__ 9 """ T.count(value) -> integer -- return number of occurrences of value """ 10 return 0 11 12 def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__ 13 """ 14 T.index(value, [start, [stop]]) -> integer -- return first index of value. 15 Raises ValueError if the value is not present. 16 """ 17 return 0 18 19 def __add__(self, y): # real signature unknown; restored from __doc__ 20 """ x.__add__(y) <==> x+y """ 21 pass 22 23 def __contains__(self, y): # real signature unknown; restored from __doc__ 24 """ x.__contains__(y) <==> y in x """ 25 pass 26 27 def __eq__(self, y): # real signature unknown; restored from __doc__ 28 """ x.__eq__(y) <==> x==y """ 29 pass 30 31 def __getattribute__(self, name): # real signature unknown; restored from __doc__ 32 """ x.__getattribute__('name') <==> x.name """ 33 pass 34 35 def __getitem__(self, y): # real signature unknown; restored from __doc__ 36 """ x.__getitem__(y) <==> x[y] """ 37 pass 38 39 def __getnewargs__(self, *args, **kwargs): # real signature unknown 40 pass 41 42 def __getslice__(self, i, j): # real signature unknown; restored from __doc__ 43 """ 44 x.__getslice__(i, j) <==> x[i:j] 45 46 Use of negative indices is not supported. 47 """ 48 pass 49 50 def __ge__(self, y): # real signature unknown; restored from __doc__ 51 """ x.__ge__(y) <==> x>=y """ 52 pass 53 54 def __gt__(self, y): # real signature unknown; restored from __doc__ 55 """ x.__gt__(y) <==> x>y """ 56 pass 57 58 def __hash__(self): # real signature unknown; restored from __doc__ 59 """ x.__hash__() <==> hash(x) """ 60 pass 61 62 def __init__(self, seq=()): # known special case of tuple.__init__ 63 """ 64 tuple() -> empty tuple 65 tuple(iterable) -> tuple initialized from iterable's items 66 67 If the argument is a tuple, the return value is the same object. 68 # (copied from class doc) 69 """ 70 pass 71 72 def __iter__(self): # real signature unknown; restored from __doc__ 73 """ x.__iter__() <==> iter(x) """ 74 pass 75 76 def __len__(self): # real signature unknown; restored from __doc__ 77 """ x.__len__() <==> len(x) """ 78 pass 79 80 def __le__(self, y): # real signature unknown; restored from __doc__ 81 """ x.__le__(y) <==> x<=y """ 82 pass 83 84 def __lt__(self, y): # real signature unknown; restored from __doc__ 85 """ x.__lt__(y) <==> x<y """ 86 pass 87 88 def __mul__(self, n): # real signature unknown; restored from __doc__ 89 """ x.__mul__(n) <==> x*n """ 90 pass 91 92 @staticmethod # known case of __new__ 93 def __new__(S, *more): # real signature unknown; restored from __doc__ 94 """ T.__new__(S, ...) -> a new object with type S, a subtype of T """ 95 pass 96 97 def __ne__(self, y): # real signature unknown; restored from __doc__ 98 """ x.__ne__(y) <==> x!=y """ 99 pass 100 101 def __repr__(self): # real signature unknown; restored from __doc__ 102 """ x.__repr__() <==> repr(x) """ 103 pass 104 105 def __rmul__(self, n): # real signature unknown; restored from __doc__ 106 """ x.__rmul__(n) <==> n*x """ 107 pass 108 109 def __sizeof__(self): # real signature unknown; restored from __doc__ 110 """ T.__sizeof__() -- size of T in memory, in bytes """ 111 pass 112 113 tuple
#元祖
#元祖有什么,列表就有什么,列表有的元祖就不一定有
1 tu = tuple((11,22,33,44,)) 2 把一个列表转换成元祖 3 tu = tuple([11,22,33,44,]) 4 print(type(tu)) 5 打印 6 <class 'tuple'> 7 8 9 如果要把一个元祖转换成列表 10 tu = list([11,22,33,44,]) 11 print(type(tu)) 12 打印 13 <class 'list'>
七、字典
如:{'name': 'wupeiqi', 'age': 18} 、{'host': '2.2.2.2', 'port': 80]}
ps:循环时,默认循环key
每个字典都具备如下功能:
1 class dict(object): 2 """ 3 dict() -> new empty dictionary 4 dict(mapping) -> new dictionary initialized from a mapping object's 5 (key, value) pairs 6 dict(iterable) -> new dictionary initialized as if via: 7 d = {} 8 for k, v in iterable: 9 d[k] = v 10 dict(**kwargs) -> new dictionary initialized with the name=value pairs 11 in the keyword argument list. For example: dict(one=1, two=2) 12 """ 13 14 def clear(self): # real signature unknown; restored from __doc__ 15 """ 清除内容 """ 16 """ D.clear() -> None. Remove all items from D. """ 17 pass 18 19 def copy(self): # real signature unknown; restored from __doc__ 20 """ 浅拷贝 """ 21 """ D.copy() -> a shallow copy of D """ 22 pass 23 24 @staticmethod # known case 25 def fromkeys(S, v=None): # real signature unknown; restored from __doc__ 26 """ 27 dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v. 28 v defaults to None. 29 """ 30 pass 31 32 def get(self, k, d=None): # real signature unknown; restored from __doc__ 33 """ 根据key获取值,d是默认值 """ 34 """ D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None. """ 35 pass 36 37 def has_key(self, k): # real signature unknown; restored from __doc__ 38 """ 是否有key """ 39 """ D.has_key(k) -> True if D has a key k, else False """ 40 return False 41 42 def items(self): # real signature unknown; restored from __doc__ 43 """ 所有项的列表形式 """ 44 """ D.items() -> list of D's (key, value) pairs, as 2-tuples """ 45 return [] 46 47 def iteritems(self): # real signature unknown; restored from __doc__ 48 """ 项可迭代 """ 49 """ D.iteritems() -> an iterator over the (key, value) items of D """ 50 pass 51 52 def iterkeys(self): # real signature unknown; restored from __doc__ 53 """ key可迭代 """ 54 """ D.iterkeys() -> an iterator over the keys of D """ 55 pass 56 57 def itervalues(self): # real signature unknown; restored from __doc__ 58 """ value可迭代 """ 59 """ D.itervalues() -> an iterator over the values of D """ 60 pass 61 62 def keys(self): # real signature unknown; restored from __doc__ 63 """ 所有的key列表 """ 64 """ D.keys() -> list of D's keys """ 65 return [] 66 67 def pop(self, k, d=None): # real signature unknown; restored from __doc__ 68 """ 获取并在字典中移除 """ 69 """ 70 D.pop(k[,d]) -> v, remove specified key and return the corresponding value. 71 If key is not found, d is returned if given, otherwise KeyError is raised 72 """ 73 pass 74 75 def popitem(self): # real signature unknown; restored from __doc__ 76 """ 获取并在字典中移除 """ 77 """ 78 D.popitem() -> (k, v), remove and return some (key, value) pair as a 79 2-tuple; but raise KeyError if D is empty. 80 """ 81 pass 82 83 def setdefault(self, k, d=None): # real signature unknown; restored from __doc__ 84 """ 如果key不存在,则创建,如果存在,则返回已存在的值且不修改 """ 85 """ D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D """ 86 pass 87 88 def update(self, E=None, **F): # known special case of dict.update 89 """ 更新 90 {'name':'alex', 'age': 18000} 91 [('name','sbsbsb'),] 92 """ 93 """ 94 D.update([E, ]**F) -> None. Update D from dict/iterable E and F. 95 If E present and has a .keys() method, does: for k in E: D[k] = E[k] 96 If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v 97 In either case, this is followed by: for k in F: D[k] = F[k] 98 """ 99 pass 100 101 def values(self): # real signature unknown; restored from __doc__ 102 """ 所有的值 """ 103 """ D.values() -> list of D's values """ 104 return [] 105 106 def viewitems(self): # real signature unknown; restored from __doc__ 107 """ 所有项,只是将内容保存至view对象中 """ 108 """ D.viewitems() -> a set-like object providing a view on D's items """ 109 pass 110 111 def viewkeys(self): # real signature unknown; restored from __doc__ 112 """ D.viewkeys() -> a set-like object providing a view on D's keys """ 113 pass 114 115 def viewvalues(self): # real signature unknown; restored from __doc__ 116 """ D.viewvalues() -> an object providing a view on D's values """ 117 pass 118 119 def __cmp__(self, y): # real signature unknown; restored from __doc__ 120 """ x.__cmp__(y) <==> cmp(x,y) """ 121 pass 122 123 def __contains__(self, k): # real signature unknown; restored from __doc__ 124 """ D.__contains__(k) -> True if D has a key k, else False """ 125 return False 126 127 def __delitem__(self, y): # real signature unknown; restored from __doc__ 128 """ x.__delitem__(y) <==> del x[y] """ 129 pass 130 131 def __eq__(self, y): # real signature unknown; restored from __doc__ 132 """ x.__eq__(y) <==> x==y """ 133 pass 134 135 def __getattribute__(self, name): # real signature unknown; restored from __doc__ 136 """ x.__getattribute__('name') <==> x.name """ 137 pass 138 139 def __getitem__(self, y): # real signature unknown; restored from __doc__ 140 """ x.__getitem__(y) <==> x[y] """ 141 pass 142 143 def __ge__(self, y): # real signature unknown; restored from __doc__ 144 """ x.__ge__(y) <==> x>=y """ 145 pass 146 147 def __gt__(self, y): # real signature unknown; restored from __doc__ 148 """ x.__gt__(y) <==> x>y """ 149 pass 150 151 def __init__(self, seq=None, **kwargs): # known special case of dict.__init__ 152 """ 153 dict() -> new empty dictionary 154 dict(mapping) -> new dictionary initialized from a mapping object's 155 (key, value) pairs 156 dict(iterable) -> new dictionary initialized as if via: 157 d = {} 158 for k, v in iterable: 159 d[k] = v 160 dict(**kwargs) -> new dictionary initialized with the name=value pairs 161 in the keyword argument list. For example: dict(one=1, two=2) 162 # (copied from class doc) 163 """ 164 pass 165 166 def __iter__(self): # real signature unknown; restored from __doc__ 167 """ x.__iter__() <==> iter(x) """ 168 pass 169 170 def __len__(self): # real signature unknown; restored from __doc__ 171 """ x.__len__() <==> len(x) """ 172 pass 173 174 def __le__(self, y): # real signature unknown; restored from __doc__ 175 """ x.__le__(y) <==> x<=y """ 176 pass 177 178 def __lt__(self, y): # real signature unknown; restored from __doc__ 179 """ x.__lt__(y) <==> x<y """ 180 pass 181 182 @staticmethod # known case of __new__ 183 def __new__(S, *more): # real signature unknown; restored from __doc__ 184 """ T.__new__(S, ...) -> a new object with type S, a subtype of T """ 185 pass 186 187 def __ne__(self, y): # real signature unknown; restored from __doc__ 188 """ x.__ne__(y) <==> x!=y """ 189 pass 190 191 def __repr__(self): # real signature unknown; restored from __doc__ 192 """ x.__repr__() <==> repr(x) """ 193 pass 194 195 def __setitem__(self, i, y): # real signature unknown; restored from __doc__ 196 """ x.__setitem__(i, y) <==> x[i]=y """ 197 pass 198 199 def __sizeof__(self): # real signature unknown; restored from __doc__ 200 """ D.__sizeof__() -> size of D in memory, in bytes """ 201 pass 202 203 __hash__ = None 204 205 dict
字典
创建两个字典
1 dic = {'k1':'v1'} 2 print(dic) 3 print(type(dic)) 4 dic = dict(k1='v1',k2='v2') 5 print(dic) 6 print(type(dic))
#清空所有的元素
1 def clear(self): # real signature unknown; restored from __doc__ 2 """ D.clear() -> None. Remove all items from D. """ 3 pass 4 5 dic = dict(k1='v1',k2='v2') 6 print(dic) 7 print(type(dic)) 8 dic.clear() 9 print(dic) 10 打印 11 {'k2': 'v2', 'k1': 'v1'} 12 <class 'dict'> 13 {}
#浅拷贝
1 def copy(self): # real signature unknown; restored from __doc__ 2 """ D.copy() -> a shallow copy of D """ 3 pass
#拿到一个key 指定一个value 生成一个新的字典
1 def fromkeys(*args, **kwargs): # real signature unknown 2 """ Returns a new dict with keys from iterable and values equal to value. """ 3 pass 4 5 #拿到一个key 指定一个value 生成一个新的字典 6 dic = dict(k1='v1',k2='v2') 7 print(dic) 8 print(type(dic)) 9 new_dic = dic.fromkeys(['k1','k9'],'v9') 10 print(new_dic) 11 打印 12 {'k2': 'v2', 'k1': 'v1'} 13 <class 'dict'> 14 {'k9': 'v9', 'k1': 'v9'}
1 def get(self, k, d=None): # real signature unknown; restored from __doc__ 2 """ D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None. """ 3 pass 4 5 dic = dict(k1='v1',k2='v2') 6 print(dic['k1']) 7 print(dic['k2']) 8 9 print(dic['k3']) 10 打印 11 v1 12 v2 13 14 Traceback (most recent call last): 15 File "E:/python/day1/day3/练习day3.py", line 10, in <module> 16 print(dic['k3']) 17 #提示 键 错误,意思说明没有这个键, 18 KeyError: 'k3' 19 ------------------------------------------ 20 #如果键 不存在的话get 会提示为None 21 dic = dict(k1='v1',k2='v2') 22 print(dic.get('k1')) 23 print(dic.get('k2')) 24 print(dic.get('k3')) 25 打印 26 v1 27 v2 28 None 29 ------------------------------------------ 30 #只要key不在的的时候,可以指定返回默认信息 31 dic = dict(k1='v1',k2='v2') 32 print(dic.get('k1')) 33 print(dic.get('k2')) 34 print(dic.get('k3')) 35 print(dic.get('k3','不在')) 36 打印 37 v1 38 v2 39 None 40 不在
1 def items(self): # real signature unknown; restored from __doc__ 2 """ D.items() -> a set-like object providing a view on D's items """ 3 pass 4 5 dic = dict(k1='v1',k2='v2') 6 #获取到的key值,也就是字典内所有的key 7 print(dic.keys()) 8 #获取到的values值,也就是字典内的所有的values 9 print(dic.values()) 10 #获取字典的所有的 键值对,也就是字典内的所有的key,和values 11 print(dic.items()) 12 打印 13 dict_keys(['k1', 'k2']) 14 dict_values(['v1', 'v2']) 15 dict_items([('k1', 'v1'), ('k2', 'v2')]) 16 ------------------------------------------- 17 dic = dict(k1='v1',k2='v2') 18 for k in dic.keys(): 19 #打印所有的key 20 print(k) 21 打印 22 k2 23 k1 24 ----------------------- 25 for v in dic.values(): 26 #打印所有的values 27 print(v) 28 打印 29 v2 30 v1 31 ----------------------- 32 for k,v in dic.items(): 33 #打印所有的key 和values 34 print(k,v) 35 打印 36 k2 v2 37 k1 v1
#拿走一个并赋值
1 def pop(self, k, d=None): # real signature unknown; restored from __doc__ 2 """ 3 D.pop(k[,d]) -> v, remove specified key and return the corresponding value. 4 If key is not found, d is returned if given, otherwise KeyError is raised 5 """ 6 pass 7 #字典是无序的 8 dic = dict(k1='v1',k2='v2') 9 #字典是无序的如果这里的pop不指定参数会报错 10 dic.pop() 11 print(dic) 12 打印 13 #这里提示需要最少指定一个参数 14 TypeError: pop expected at least 1 arguments, got 0 15 ---------------------------------------- 16 可以看到指定了 k1 然后就把 k1 拿走了, 17 dic = dict(k1='v1',k2='v2') 18 new_dic = dic.pop(‘k1') 19 print(dic) 20 print(new_dic) 21 打印 22 {'k2': 'v2'}
#随机删除一个
1 #随机删除一个 2 def popitem(self): # real signature unknown; restored from __doc__ 3 """ 4 D.popitem() -> (k, v), remove and return some (key, value) pair as a 5 2-tuple; but raise KeyError if D is empty. 6 """ 7 pass
#在没有values 的情况 下默认会等于None
1 def setdefault(self, k, d=None): # real signature unknown; restored from __doc__ 2 """ D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D """ 3 pass 4 dic = dict(k1='v1',k2='v2') 5 dic['k3'] = 123 6 dic.setdefault('k8') 7 print(dic) 8 打印 9 {'k2': 'v2', 'k8': None, 'k1': 'v1', 'k3': 123}
#更新这个字典,
1 def update(self, E=None, **F): # known special case of dict.update 2 """ 3 D.update([E, ]**F) -> None. Update D from dict/iterable E and F. 4 If E is present and has a .keys() method, then does: for k in E: D[k] = E[k] 5 If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v 6 In either case, this is followed by: for k in F: D[k] = F[k] 7 """ 8 pass 9 10 dic = dict(k1='v1',k2='v2') 11 dic.update({'k7':999}) 12 print(dic) 13 打印 14 {'k2': 'v2', 'k1': 'v1', 'k7': 999}
小游戏:
1 [11,22,33,44,55,66,77,88,99] 2 把大于66 的值保存到字典的可一个key中,将小于66的值保存到第二个key的值中 3 即:{’k1‘:大于66,’k2':小于66 } 4 5 条件: 6 放在 dic = {} 字典里 7 all_list = [11,22,33,44,55,66,77,88,99] 8 dic = {} 9 l1 = [] 10 l2 = [] 11 for i in all_list: 12 if i > 66: 13 #print(i) 14 l1.append(i) 15 #print(l1) 16 else: 17 l2.append(i) 18 dic['k1'] = l1 19 dic['k2'] = l2 20 print(dic) 21 ----------------------------- 22 all_list = [11,22,33,44,55,66,77,88,99] 23 dic = {} 24 for i in all_list: 25 if i > 66: 26 if "k1" in dic.keys(): 27 dic['k1'].append(i) 28 else: 29 dic['k1'] = [i,] 30 if i <= 66: 31 if "k2" in dic.keys(): 32 dic['k2'].append(i) 33 else: 34 dic['k2'] = [i,] 35 36 print(dic)