Built-in Types内置类型¶
The following sections describe the standard types that are built into the interpreter.以下部分描述了内置到解释器中的标准类型。
The principal built-in types are numerics, sequences, mappings, classes, instances and exceptions.主要的内置类型是数字、序列、映射、类、实例和异常。
Some collection classes are mutable. 某些集合类是可变的。The methods that add, subtract, or rearrange their members in place, and don’t return a specific item, never return the collection instance itself but 添加、减去或重新排列其成员的方法不会返回特定项,也不会返回集合实例本身,但会返回None
.None
。
Some operations are supported by several object types; in particular, practically all objects can be compared for equality, tested for truth value, and converted to a string (with the 一些操作由多个对象类型支持;特别是,几乎所有对象都可以进行相等性比较,测试真值,并转换为字符串(使用repr()
function or the slightly different str()
function). repr()
函数或略有不同的str()
函数)。The latter function is implicitly used when an object is written by the 当print()
function.print()
函数写入对象时,隐式使用后一个函数。
Truth Value Testing真值检验¶
Any object can be tested for truth value, for use in an 任何对象都可以测试真值,用于if
or while
condition or as operand of the Boolean operations below.if
或while
条件,或作为下面布尔运算的操作数。
By default, an object is considered true unless its class defines either a 默认情况下,对象被视为__bool__()
method that returns False
or a __len__()
method that returns zero, when called with the object. true
,除非其类定义了返回False
的__bool__()
方法或与对象一起调用时返回零的__len__()
方法。1 Here are most of the built-in objects considered false:以下是大多数被认为是错误的内置对象:
constants defined to be false:定义为None
andFalse
.false
的常量:None
和false
。zero of any numeric type:任何数字类型的零:0
,0.0
,0j
,Decimal(0)
,Fraction(0, 1)
0
、0.0
、0j
、Decimal(0)
、Fraction(0, 1)
empty sequences and collections:空序列和集合:''
,()
,[]
,{}
,set()
,range(0)
''
、()
、[]
、{}
、set()
、range(0)
Operations and built-in functions that have a Boolean result always return 除非另有说明,否则具有布尔结果的操作和内置函数始终返回0
or False
for false and 1
or True
for true, unless otherwise stated. 0
或False
表示False
,返回1
或True
表示True
。(Important exception: the Boolean operations (重要例外:布尔运算or
and and
always return one of their operands.)or
和and
始终返回其中一个操作数。)
Boolean Operations — and
, or
, not
布尔运算:and
、or
、not
¶
and
, or
, not
These are the Boolean operations, ordered by ascending priority:以下是布尔运算,按优先级升序排列:
|
|
|
---|---|---|
|
|
(1) |
|
|
(2) |
|
|
(3) |
Notes:
This is a short-circuit operator, so it only evaluates the second argument if the first one is false.这是一个短路运算符,因此仅当第一个参数为false
时才计算第二个参数。This is a short-circuit operator, so it only evaluates the second argument if the first one is true.这是一个短路运算符,因此它仅在第一个参数为true
时计算第二个参数。not
has a lower priority than non-Boolean operators, sonot a == b
is interpreted asnot (a == b)
, anda == not b
is a syntax error.not
的优先级低于非布尔运算符,因此not a==b
被解释为not (a == b)
,a == not b
是一个语法错误。
Comparisons比较¶
There are eight comparison operations in Python. Python中有八个比较操作。They all have the same priority (which is higher than that of the Boolean operations). 它们都具有相同的优先级(高于布尔运算的优先级)。Comparisons can be chained arbitrarily; for example, 可以任意链接比较;例如,x < y <= z
is equivalent to x < y and y <= z
, except that y is evaluated only once (but in both cases z is not evaluated at all when x < y
is found to be false).x < y <= z
等同于x < y and y <= z
,只是y只计算一次(但在这两种情况下,当x<y
为false
时,z
根本不计算)。
This table summarizes the comparison operations:此表总结了比较操作:
|
|
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Objects of different types, except different numeric types, never compare equal. 不同类型的对象(不同的数字类型除外)从不进行相等比较。The 始终定义==
operator is always defined but for some object types (for example, class objects) is equivalent to is
. ==
运算符,但对于某些对象类型(例如,类对象),它等效于is
。The <
, <=
, >
and >=
operators are only defined where they make sense; for example, they raise a TypeError
exception when one of the arguments is a complex number.<
、<=
、>
和>=
运算符仅在有意义的地方定义;例如,当其中一个参数是复数时,它们会引发TypeError
异常。
Non-identical instances of a class normally compare as non-equal unless the class defines the 除非类定义了__eq__()
method.__eq__()
方法,否则类的不相同实例通常比较为不相等。
Instances of a class cannot be ordered with respect to other instances of the same class, or other types of object, unless the class defines enough of the methods 一个类的实例不能相对于同一个类的其他实例或其他类型的对象排序,除非该类定义了足够多的方法__lt__()
, __le__()
, __gt__()
, and __ge__()
(in general, __lt__()
and __eq__()
are sufficient, if you want the conventional meanings of the comparison operators).__lt__()
、__le__()
、__gt__()
、和__ge__()
。
The behavior of the 不能自定义is
and is not
operators cannot be customized; also they can be applied to any two objects and never raise an exception.is
和is not
运算符的行为;此外,它们可以应用于任意两个对象,并且不会引发异常。
Two more operations with the same syntactic priority, iterable类型或实现in
and not in
, are supported by types that are iterable or implement the __contains__()
method.__contains__()
方法的类型支持另外两个具有相同语法优先级的操作,in
和not in
。
Numeric Types数值类型:int
, float
, complex
¶
There are three distinct numeric types: integers, floating point numbers, and complex numbers. 有三种不同的数字类型:整数、浮点数和复数。In addition, Booleans are a subtype of integers. 此外,布尔是整数的一个子类型。Integers have unlimited precision. 整数具有无限精度。Floating point numbers are usually implemented using double in C; information about the precision and internal representation of floating point numbers for the machine on which your program is running is available in 浮点数通常在C中使用double实现;sys.float_info
. sys.float_info
中提供了有关运行程序的计算机的浮点数精度和内部表示形式的信息。Complex numbers have a real and imaginary part, which are each a floating point number. 复数有实部和虚部,每一部分都是浮点数。To extract these parts from a complex number z, use 要从复数z中提取这些部分,请使用z.real
and z.imag
. z.real
和z.imag
。(The standard library includes the additional numeric types (标准库包括额外的数字类型fractions.Fraction
, for rationals, and decimal.Decimal
, for floating-point numbers with user-definable precision.)fractions.Fraction
,用于有理数,decimal.Decimal
,用于具有用户定义精度的浮点数。)
Numbers are created by numeric literals or as the result of built-in functions and operators. 数字由数字文字或内置函数和运算符创建。Unadorned integer literals (including hex, octal and binary numbers) yield integers. 未加修饰的整数文本(包括十六进制、八进制和二进制数)生成整数。Numeric literals containing a decimal point or an exponent sign yield floating point numbers. 包含小数点或指数符号的数字文本生成浮点数。Appending 将'j'
or 'J'
to a numeric literal yields an imaginary number (a complex number with a zero real part) which you can add to an integer or float to get a complex number with real and imaginary parts.'j'
或'J'
附加到数字文字上会产生一个虚数(实数部分为零的复数),可以将其添加到整数或浮点中,以获得实数部分和虚数部分的复数。
Python fully supports mixed arithmetic: when a binary arithmetic operator has operands of different numeric types, the operand with the “narrower” type is widened to that of the other, where integer is narrower than floating point, which is narrower than complex. Python完全支持混合算术:当二元算术运算符具有不同数字类型的操作数时,具有“窄”类型的操作数将扩展到另一种类型的操作数,其中整数比浮点窄,浮点比复数窄。A comparison between numbers of different types behaves as though the exact values of those numbers were being compared. 不同类型的数字之间的比较表现为这些数字的精确值正在进行比较。2
The constructors 构造函数int()
, float()
, and complex()
can be used to produce numbers of a specific type.int()
、int()
和complex()
可用于生成特定类型的数字。
All numeric types (except complex) support the following operations (for priorities of the operations, see Operator precedence):所有数字类型(复杂类型除外)都支持以下操作(有关操作的优先级,请参阅运算符优先级):
|
|
|
|
---|---|---|---|
|
|
||
|
|
||
|
|
||
|
|
||
|
|
(1) |
|
|
|
(2) |
|
|
|
||
|
x |
||
|
|
||
|
x |
(3)(6) |
|
|
x |
(4)(6) |
|
|
|
(6) |
|
|
|
||
|
|
(2) |
|
|
|
(5) |
|
|
|
(5) |
Notes:
Also referred to as integer division.也称为整数除法。The resultant value is a whole integer, though the result’s type is not necessarily int.结果值是一个整数,尽管结果的类型不一定是int。The result is always rounded towards minus infinity:结果总是向负无穷大舍入:1//2
is0
,(-1)//2
is-1
,1//(-2)
is-1
, and(-1)//(-2)
is0
.1//2
为0
,(-1)//2
为-1
,1//(-2)
为-1
,(-1)//(-2)
为0
。Not for complex numbers.不适用于复数。Instead convert to floats using如果合适的话,可以使用abs()
if appropriate.abs()
转换为浮点数。-
Conversion from floating point to integer may round or truncate as in C; see functions从浮点到整数的转换可以像C中那样进行舍入或截断;有关定义良好的转换,请参阅函数math.floor()
andmath.ceil()
for well-defined conversions.math.floor()
和math.ceil()
。 float also accepts the strings “nan” and “inf” with an optional prefix “+” or “-” for Not a Number (NaN) and positive or negative infinity.浮点数还接受字符串“nan”和“inf”,并带有可选前缀“+”或“-”,表示非数字(NaN)和正无穷大或负无穷大。Python definesPython将pow(0, 0)
and0 ** 0
to be1
, as is common for programming languages.pow(0, 0)
和0**0
定义为1
,这对于编程语言来说很常见。The numeric literals accepted include the digits接受的数字文字包括数字0
to9
or any Unicode equivalent (code points with theNd
property).0
到9
或任何等效的Unicode(具有Nd
属性的代码点)。See https://www.unicode.org/Public/13.0.0/ucd/extracted/DerivedNumericType.txt for a complete list of code points with the请参阅https://www.unicode.org/Public/13.0.0/ucd/extracted/DerivedNumericType.txt获取具有Nd
property.Nd
属性的代码点的完整列表。
All 所有numbers.Real
types (int
and float
) also include the following operations:numbers.Real
类型(int
和float
)还包括以下操作:
|
|
---|---|
x |
|
For additional numeric operations see the 有关其他数值操作,请参阅math
and cmath
modules.math
和cmath
模块。
Bitwise Operations on Integer Types整数类型的位运算¶
Bitwise operations only make sense for integers. 按位运算只对整数有意义。The result of bitwise operations is calculated as though carried out in two’s complement with an infinite number of sign bits.按位运算的结果被计算出来,就好像是用无限多个符号位在二的补码中执行一样。
The priorities of the binary bitwise operations are all lower than the numeric operations and higher than the comparisons; the unary operation 二进制位运算的优先级均低于数值运算,高于比较运算;一元运算~
has the same priority as the other unary numeric operations (+
and -
).~
与其他一元数字运算(+
和-
)具有相同的优先级。
This table lists the bitwise operations sorted in ascending priority:此表列出了按升序优先级排序的按位操作:
|
|
|
---|---|---|
|
|
(4) |
|
|
(4) |
|
|
(4) |
|
|
(1)(2) |
|
|
(1)(3) |
|
|
Notes:
Negative shift counts are illegal and cause a负移位计数非法,并导致引发ValueError
to be raised.ValueError
。A left shift by n bits is equivalent to multiplication by左移n位相当于乘以pow(2, n)
.pow(2, n)
。A right shift by n bits is equivalent to floor division by右移n位相当于按pow(2, n)
.pow(2, n)
进行地板除法。Performing these calculations with at least one extra sign extension bit in a finite two’s complement representation (a working bit-width of在有限2的补码表示(工作位宽度为1 + max(x.bit_length(), y.bit_length())
or more) is sufficient to get the same result as if there were an infinite number of sign bits.1 + max(x.bit_length(), y.bit_length())
或更大)中,使用至少一个额外的符号扩展位执行这些计算足以获得与无限个符号位相同的结果。
Additional Methods on Integer Types关于整数类型的其他方法¶
The int type implements the int类型实现numbers.Integral
abstract base class. numbers.Integral
抽象基类。In addition, it provides a few more methods:此外,它还提供了一些其他方法:
-
int.
bit_length
()¶ Return the number of bits necessary to represent an integer in binary, excluding the sign and leading zeros:返回用二进制表示整数所需的位数,不包括符号和前导零:>>> n = -37
>>> bin(n)
'-0b100101'
>>> n.bit_length()
6More precisely, if更准确地说,如果x
is nonzero, thenx.bit_length()
is the unique positive integerk
such that2**(k-1) <= abs(x) < 2**k
.x
非零,那么x.bit_length()
是唯一的正整数k
,使得2**(k-1) <= abs(x) < 2**k
。Equivalently, when等效地,当abs(x)
is small enough to have a correctly rounded logarithm, thenk = 1 + int(log(abs(x), 2))
.abs(x)
足够小,可以得到正确的四舍五入对数时,则k = 1 + int(log(abs(x), 2))
。If如果x
is zero, thenx.bit_length()
returns0
.x
为零,则x.bit_length()
返回0。Equivalent to:相当于:def bit_length(self):
s = bin(self) # binary representation: bin(-37) --> '-0b100101'
s = s.lstrip('-0b') # remove leading zeros and minus sign
return len(s) # len('100101') --> 6New in version 3.1.版本3.1中新增。
-
int.
bit_count
()¶ Return the number of ones in the binary representation of the absolute value of the integer.返回整数绝对值的二进制表示形式中的位数。This is also known as the population count.这也称为人口计数。Example:示例:>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3Equivalent to:相当于:def bit_count(self):
return bin(self).count("1")New in version 3.10.版本3.10中新增。
-
int.
to_bytes
(length, byteorder, *, signed=False)¶ Return an array of bytes representing an integer.返回表示整数的字节数组。>>> (1024).to_bytes(2, byteorder='big')
b'\x04\x00'
>>> (1024).to_bytes(10, byteorder='big')
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> (-1024).to_bytes(10, byteorder='big', signed=True)
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes((x.bit_length() + 7) // 8, byteorder='little')
b'\xe8\x03'The integer is represented using length bytes.整数使用length字节表示。An如果整数不能用给定的字节数表示,则会引发OverflowError
is raised if the integer is not representable with the given number of bytes.OverflowError
。The byteorder argument determines the byte order used to represent the integer.byteorder参数确定用于表示整数的字节顺序。If byteorder is如果byteorder为"big"
, the most significant byte is at the beginning of the byte array."big"
,则最高有效字节位于字节数组的开头。If byteorder is如果byteorder为"little"
, the most significant byte is at the end of the byte array."little"
,则最高有效字节位于字节数组的末尾。To request the native byte order of the host system, use要请求主机系统的本机字节顺序,请使用sys.byteorder
as the byte order value.sys.byteorder
作为字节顺序值。The signed argument determines whether two’s complement is used to represent the integer.signed参数确定是否使用2的补码来表示整数。If signed is如果signed为False
and a negative integer is given, anOverflowError
is raised.False
,并且给出了一个负整数,则会引发OverflowError
。The default value for signed issigned的默认值为False
.False
。New in version 3.2.版本3.2中新增。
-
classmethod
int.
from_bytes
(bytes, byteorder, *, signed=False)¶ Return the integer represented by the given array of bytes.返回由给定字节数组表示的整数。>>> int.from_bytes(b'\x00\x10', byteorder='big')
16
>>> int.from_bytes(b'\x00\x10', byteorder='little')
4096
>>> int.from_bytes(b'\xfc\x00', byteorder='big', signed=True)
-1024
>>> int.from_bytes(b'\xfc\x00', byteorder='big', signed=False)
64512
>>> int.from_bytes([255, 0, 0], byteorder='big')
16711680The argument bytes must either be a bytes-like object or an iterable producing bytes.参数bytes必须是类字节对象或生成字节的iterable。The byteorder argument determines the byte order used to represent the integer.byteorder参数确定用于表示整数的字节顺序。If byteorder is如果byteorder为"big"
, the most significant byte is at the beginning of the byte array."big"
,则最高有效字节位于字节数组的开头。If byteorder is如果byteorder为"little"
, the most significant byte is at the end of the byte array."little"
,则最高有效字节位于字节数组的末尾。To request the native byte order of the host system, use要请求主机系统的本机字节顺序,请使用sys.byteorder
as the byte order value.sys.byteorder
作为字节顺序值。The signed argument indicates whether two’s complement is used to represent the integer.signed参数指示是否使用2的补码来表示整数。New in version 3.2.版本3.2中新增。
-
int.
as_integer_ratio
()¶ Return a pair of integers whose ratio is exactly equal to the original integer and with a positive denominator.返回一对整数,其比率正好等于原始整数,且分母为正。The integer ratio of integers (whole numbers) is always the integer as the numerator and整数(整数)的整数比始终是整数作为分子,1
as the denominator.1
作为分母。New in version 3.8.版本3.8中新增。
Additional Methods on Float浮点数上的其他方法¶
The float type implements the 浮点数类型实现numbers.Real
abstract base class. numbers.Real
抽象基类。float also has the following additional methods.浮点数还有以下附加方法。
-
float.
as_integer_ratio
()¶ Return a pair of integers whose ratio is exactly equal to the original float and with a positive denominator.返回一对整数,其比率正好等于原始浮点,且分母为正。Raises在无穷大上引发OverflowError
on infinities and aValueError
on NaNs.OverflowError
,在NaN上引发ValueError
。
-
float.
is_integer
()¶ Return如果浮点数实例是带整数值的有限实例,则返回True
if the float instance is finite with integral value, andFalse
otherwise:True
,否则返回False
:>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
Two methods support conversion to and from hexadecimal strings. 有两种方法支持十六进制字符串之间的转换。Since Python’s floats are stored internally as binary numbers, converting a float to or from a decimal string usually involves a small rounding error. 由于Python的浮点数在内部存储为二进制数,因此将浮点数转换为decimal字符串或从decimal字符串转换为浮点数通常会产生较小的舍入误差。In contrast, hexadecimal strings allow exact representation and specification of floating-point numbers. 相反,十六进制字符串允许浮点数的精确表示和指定。This can be useful when debugging, and in numerical work.这在调试和数值计算工作中非常有用。
-
float.
hex
()¶ Return a representation of a floating-point number as a hexadecimal string.返回浮点数的十六进制字符串表示形式。For finite floating-point numbers, this representation will always include a leading对于有限浮点数,此表示将始终包括前导0x
and a trailingp
and exponent.0x
和尾随p
和指数。
-
classmethod
float.
fromhex
(s)¶ Class method to return the float represented by a hexadecimal string s.类方法返回由十六进制字符串s表示的浮点。The string s may have leading and trailing whitespace.字符串s可能有前导和尾随空格。
Note that 请注意,float.hex()
is an instance method, while float.fromhex()
is a class method.float.hex()
是一个实例方法,而float.fromhex()
是一个类方法。
A hexadecimal string takes the form:十六进制字符串的格式为:
[sign] ['0x'] integer ['.' fraction] ['p' exponent]
where the optional 其中可选sign
may by either +
or -
, integer
and fraction
are strings of hexadecimal digits, and exponent
is a decimal integer with an optional leading sign. sign
可以是+
或-
,integer
和fraction
是十六进制数字的字符串,exponent
是带可选前导符号的十进制整数。Case is not significant, and there must be at least one hexadecimal digit in either the integer or the fraction. 大小写不重要,整数或分数中必须至少有一个十六进制数字。This syntax is similar to the syntax specified in section 6.4.4.2 of the C99 standard, and also to the syntax used in Java 1.5 onwards. 此语法类似于C99标准第6.4.4.2节中指定的语法,也类似于Java 1.5以后版本中使用的语法。In particular, the output of 特别是,float.hex()
is usable as a hexadecimal floating-point literal in C or Java code, and hexadecimal strings produced by C’s %a
format character or Java’s Double.toHexString
are accepted by float.fromhex()
.float.hex()
的输出在C或Java代码中可用作十六进制浮点文本,并且由C的%a
格式字符或Java的Double.toHexString
生成的十六进制字符串被float.fromhex()
接受。
Note that the exponent is written in decimal rather than hexadecimal, and that it gives the power of 2 by which to multiply the coefficient. 请注意,指数是用十进制而不是十六进制表示的,它给出了系数乘以2的幂。For example, the hexadecimal string 例如,十六进制字符串0x3.a7p10
represents the floating-point number (3 + 10./16 + 7./16**2) * 2.0**10
, or 3740.0
:0x3.a7p10
表示浮点数(3 + 10./16 + 7./16**2) * 2.0**10
或3740.0
:
>>> float.fromhex('0x3.a7p10')
3740.0
Applying the reverse conversion to 将反向转换应用于3740.0
gives a different hexadecimal string representing the same number:3740.0
会得到表示相同数字的不同十六进制字符串:
>>> float.hex(3740.0)
'0x1.d380000000000p+11'
Hashing of numeric types数字类型哈希¶
For numbers 对于可能具有不同类型的数字x
and y
, possibly of different types, it’s a requirement that hash(x) == hash(y)
whenever x == y
(see the __hash__()
method documentation for more details). x
和y
,每当x==y
时,要求hash(x) == hash(y)
(有关详细信息,请参阅__hash__()
方法文档)。For ease of implementation and efficiency across a variety of numeric types (including 为了便于在各种数字类型(包括int
, float
, decimal.Decimal
and fractions.Fraction
) Python’s hash for numeric types is based on a single mathematical function that’s defined for any rational number, and hence applies to all instances of int
and fractions.Fraction
, and all finite instances of float
and decimal.Decimal
. int
、float
、decimal.Decimal
和fractions.Fraction
)之间实现和提高效率,Python的数字类型哈希基于为任何有理数定义的单个数学函数,因此适用于int
和fractions.Fraction
的所有实例,以及float
和decimal.Decimal
的所有有限实例。Essentially, this function is given by reduction modulo 本质上,这个函数是由固定素数P
for a fixed prime P
. P
的约化模P
给出的。The value of Python可以使用P
is made available to Python as the modulus
attribute of sys.hash_info
.P
的值作为sys.hash_info
的modulus
属性。
CPython implementation detail:CPython实施详情: Currently, the prime used is 目前,在具有32位C长的计算机上,使用的基本值是P = 2**31 - 1
on machines with 32-bit C longs and P = 2**61 - 1
on machines with 64-bit C longs.P = 2**31 - 1
,在具有64位C长的计算机上,使用的基本值是P = 2**61 - 1
。
Here are the rules in detail:具体规则如下:
If如果x = m / n
is a nonnegative rational number andn
is not divisible byP
, definehash(x)
asm * invmod(n, P) % P
, whereinvmod(n, P)
gives the inverse ofn
moduloP
.x=m/n
是一个非负有理数,且n
不能被P
整除,则将hash(x)
定义为m * invmod(n, P) % P
,其中invmod(n, P)
给出n
模P
的逆。If如果x = m / n
is a nonnegative rational number andn
is divisible byP
(butm
is not) thenn
has no inverse moduloP
and the rule above doesn’t apply; in this case definehash(x)
to be the constant valuesys.hash_info.inf
.x=m/n
是一个非负有理数,n
可被P
整除(但m
不是),那么n
没有逆模P
,上述规则不适用;在本例中,将hash(x)
定义为常量值sys.hash_info.inf
。If如果x = m / n
is a negative rational number definehash(x)
as-hash(-x)
.x=m/n
是负有理数,则将hash(x)
为-hash(-x)
。If the resulting hash is如果得到的哈希值是-1
, replace it with-2
.-1
,则将其替换为-2
。The particular values特定值sys.hash_info.inf
and-sys.hash_info.inf
are used as hash values for positive infinity or negative infinity (respectively).sys.hash_info.inf
和-sys.hash_info.inf
分别用作正无穷大或负无穷大的哈希值。For a对于复数complex
numberz
, the hash values of the real and imaginary parts are combined by computinghash(z.real) + sys.hash_info.imag * hash(z.imag)
, reduced modulo2**sys.hash_info.width
so that it lies inrange(-2**(sys.hash_info.width - 1), 2**(sys.hash_info.width - 1))
.z
,实数部分和虚数部分的哈希值通过计算hash(z.real) + sys.hash_info.imag * hash(z.imag)
,减去2**sys.hash_info.width
进行组合,使其位于range(-2**(sys.hash_info.width - 1), 2**(sys.hash_info.width - 1))
。Again, if the result is同样,如果结果为-1
, it’s replaced with-2
.-1
,则替换为-2
。
To clarify the above rules, here’s some example Python code, equivalent to the built-in hash, for computing the hash of a rational number, 为了澄清上述规则,下面是一些Python代码示例,相当于内置哈希,用于计算有理数、float
, or complex
:float
或complex
的哈希:
import sys, math
def hash_fraction(m, n):
"""Compute the hash of a rational number m / n.
Assumes m and n are integers, with n positive.
Equivalent to hash(fractions.Fraction(m, n)).
"""
P = sys.hash_info.modulus
# Remove common factors of P. (Unnecessary if m and n already coprime.)
while m % P == n % P == 0:
m, n = m // P, n // P
if n % P == 0:
hash_value = sys.hash_info.inf
else:
# Fermat's Little Theorem: pow(n, P-1, P) is 1, so
# pow(n, P-2, P) gives the inverse of n modulo P.
hash_value = (abs(m) % P) * pow(n, P - 2, P) % P
if m < 0:
hash_value = -hash_value
if hash_value == -1:
hash_value = -2
return hash_value
def hash_float(x):
"""Compute the hash of a float x."""
if math.isnan(x):
return object.__hash__(x)
elif math.isinf(x):
return sys.hash_info.inf if x > 0 else -sys.hash_info.inf
else:
return hash_fraction(*x.as_integer_ratio())
def hash_complex(z):
"""Compute the hash of a complex number z."""
hash_value = hash_float(z.real) + sys.hash_info.imag * hash_float(z.imag)
# do a signed reduction modulo 2**sys.hash_info.width
M = 2**(sys.hash_info.width - 1)
hash_value = (hash_value & (M - 1)) - (hash_value & M)
if hash_value == -1:
hash_value = -2
return hash_value
Iterator Types迭代器类型¶
Python supports a concept of iteration over containers. Python支持容器上的迭代概念。This is implemented using two distinct methods; these are used to allow user-defined classes to support iteration. 这是使用两种不同的方法实现的;这些用于允许用户定义的类支持迭代。Sequences, described below in more detail, always support the iteration methods.下面详细描述的序列始终支持迭代方法。
One method needs to be defined for container objects to provide iterable support:需要为容器对象定义一种方法来提供iterable支持:
-
container.
__iter__
()¶ Return an iterator object.返回iterator对象。The object is required to support the iterator protocol described below.需要该对象来支持下面描述的迭代器协议。If a container supports different types of iteration, additional methods can be provided to specifically request iterators for those iteration types.如果容器支持不同类型的迭代,那么可以提供其他方法来专门请求这些迭代类型的迭代器。(An example of an object supporting multiple forms of iteration would be a tree structure which supports both breadth-first and depth-first traversal.)(支持多种迭代形式的对象的一个示例是支持广度优先和深度优先遍历的树结构。)This method corresponds to the此方法对应于Python/C API中Python对象类型结构的tp_iter
slot of the type structure for Python objects in the Python/C API.tp_iter
插槽。
The iterator objects themselves are required to support the following two methods, which together form the iterator protocol:迭代器对象本身需要支持以下两种方法,这两种方法共同构成迭代器协议:
-
iterator.
__iter__
()¶ Return the iterator object itself.返回iterator对象本身。This is required to allow both containers and iterators to be used with the这是允许容器和迭代器与for
andin
statements.for
和in
语句一起使用所必需的。This method corresponds to the此方法对应于Python/C API中Python对象类型结构的tp_iter
slot of the type structure for Python objects in the Python/C API.tp_iter
插槽。
-
iterator.
__next__
()¶ Return the next item from the iterator.从iterator返回下一项。If there are no further items, raise the如果没有其他项,则引发StopIteration
exception.StopIteration
异常。This method corresponds to the此方法对应于Python/C API中Python对象类型结构的tp_iternext
slot of the type structure for Python objects in the Python/C API.tp_iternext
插槽。
Python defines several iterator objects to support iteration over general and specific sequence types, dictionaries, and other more specialized forms. Python定义了几个迭代器对象,以支持对一般和特定序列类型、字典和其他更特殊形式的迭代。The specific types are not important beyond their implementation of the iterator protocol.除了迭代器协议的实现之外,特定类型并不重要。
Once an iterator’s 一旦迭代器的__next__()
method raises StopIteration
, it must continue to do so on subsequent calls. __next__()
方法引发StopIteration
,它必须在后续调用中继续这样做。Implementations that do not obey this property are deemed broken.不遵守此属性的实现将被视为已损坏。
Generator Types生成器类型¶
Python’s generators provide a convenient way to implement the iterator protocol. Python的generator为实现迭代器协议提供了一种方便的方法。If a container object’s 如果容器对象的__iter__()
method is implemented as a generator, it will automatically return an iterator object (technically, a generator object) supplying the __iter__()
and __next__()
methods. __iter__()
方法实现为生成器,它将自动返回一个迭代器对象(从技术上讲,是生成器对象),提供__iter__()
和__next__()
方法。More information about generators can be found in the documentation for the yield expression.有关生成器的更多信息,请参阅yield
表达式的文档。
Sequence Types — list
, tuple
, range
序列类型:list
、tuple
、range
¶
list
, tuple
, range
There are three basic sequence types: lists, tuples, and range objects. 有三种基本的序列类型:列表、元组和范围对象。Additional sequence types tailored for processing of binary data and text strings are described in dedicated sections.专门章节中描述了为二进制数据和文本字符串处理量身定制的其他序列类型。
Common Sequence Operations常见序列操作¶
The operations in the following table are supported by most sequence types, both mutable and immutable. 大多数序列类型(可变和不可变)都支持下表中的操作。The 提供collections.abc.Sequence
ABC is provided to make it easier to correctly implement these operations on custom sequence types.collections.abc.Sequence
抽象基类可以更容易地在自定义序列类型上正确实现这些操作。
This table lists the sequence operations sorted in ascending priority. 此表列出了按升序优先级排序的序列操作。In the table, s and t are sequences of the same type, n, i, j and k are integers and x is an arbitrary object that meets any type and value restrictions imposed by s.在表中,s和t是相同类型的序列,n、i、j和k是整数,x是满足s施加的任何类型和值限制的任意对象。
The in
and not in
operations have the same priorities as the comparison operations. in
和not in
操作与比较操作具有相同的优先级。The +
(concatenation) and *
(repetition) operations have the same priority as the corresponding numeric operations. +
(串联)和*
(重复)操作与相应的数字操作具有相同的优先级。3
|
|
|
---|---|---|
|
|
(1) |
|
|
(1) |
|
|
(6)(7) |
|
|
(2)(7) |
|
|
(3) |
|
|
(3)(4) |
|
|
(3)(5) |
|
|
|
|
|
|
|
|
|
|
|
(8) |
|
|
Sequences of the same type also support comparisons. 相同类型的序列也支持比较。In particular, tuples and lists are compared lexicographically by comparing corresponding elements. 特别是,元组和列表是通过比较相应的元素按字典顺序进行比较的。This means that to compare equal, every element must compare equal and the two sequences must be of the same type and have the same length. 这意味着要比较相等,每个元素必须比较相等,并且两个序列必须具有相同的类型和长度。(For full details see Comparisons in the language reference.)(有关详细信息,请参阅语言参考中的比较。)
Forward and reversed iterators over mutable sequences access values using an index. 可变序列上的正向和反向迭代器使用索引访问值。That index will continue to march forward (or backward) even if the underlying sequence is mutated. 即使基础序列发生突变,该指数仍将继续向前(或向后)移动。The iterator terminates only when an 迭代器仅在遇到IndexError
or a StopIteration
is encountered (or when the index drops below zero).IndexError
或StopIteration
时终止(或者当索引降到零以下时)。
Notes:
While the虽然在一般情况下,in
andnot in
operations are used only for simple containment testing in the general case, some specialised sequences (such asstr
,bytes
andbytearray
) also use them for subsequence testing:in
和not in
操作仅用于简单的安全壳测试,但一些专用序列(如str
、bytes
和bytearray
)也将其用于子序列测试:>>> "gg" in "eggs"
TrueValues of n less than小于0
are treated as0
(which yields an empty sequence of the same type as s).0
的n值被视为0
(这将生成与s相同类型的空序列)。Note that items in the sequence s are not copied; they are referenced multiple times.请注意,序列s中的项目不会被复制;它们被多次引用。This often haunts new Python programmers; consider:这经常困扰着新的Python程序员;考虑:>>> lists = [[]] * 3
>>> lists
[[], [], []]
>>> lists[0].append(3)
>>> lists
[[3], [3], [3]]What has happened is that实际情况是,[[]]
is a one-element list containing an empty list, so all three elements of[[]] * 3
are references to this single empty list.[[]]
是一个包含空列表的单元素列表,因此[[]]*3
的所有三个元素都是对这个空列表的引用。Modifying any of the elements of修改列表的任何元素都会修改此单个列表。lists
modifies this single list.You can create a list of different lists this way:您可以通过以下方式创建不同列表的列表:>>> lists = [[] for i in range(3)]
>>> lists[0].append(3)
>>> lists[1].append(5)
>>> lists[2].append(7)
>>> lists
[[3], [5], [7]]Further explanation is available in the FAQ entry How do I create a multidimensional list?.FAQ条目《如何创建多维列表?》中提供了进一步的解释。If i or j is negative, the index is relative to the end of sequence s:如果i或j为负,则索引相对于序列s的结尾:len(s) + i
orlen(s) + j
is substituted.len(s)+i
或len(s)+j
被替换。But note that但请注意,-0
is still0
.-0
仍然是0
。The slice of s from i to j is defined as the sequence of items with index k such thats从i到j的切片被定义为索引为k的项目序列,使得i <= k < j
.i < =k < j
。If i or j is greater than如果i或j大于len(s)
, uselen(s)
.len(s)
,则使用len(s)
。If i is omitted or如果省略i或i为None
, use0
.None
,请使用0
。If j is omitted or如果省略j或j为None
, uselen(s)
.None
,请使用len(s)
。If i is greater than or equal to j, the slice is empty.如果i大于或等于j,则切片为空。The slice of s from i to j with step k is defined as the sequence of items with index具有步长k的s从i到j的切片被定义为索引x = i + n*k
such that0 <= n < (j-i)/k
.x = i + n*k
的项目序列,使得0 <= n < (j-i)/k
。In other words, the indices are换句话说,指数是i
,i+k
,i+2*k
,i+3*k
and so on, stopping when j is reached (but never including j).i
、i+k
、i+2*k
、i+3*k
等等,当达到j时停止(但不包括j)。When k is positive, i and j are reduced to当k为正时,如果i和j大于len(s)
if they are greater.len(s)
,则它们将减小为len(s)
。When k is negative, i and j are reduced to当k为负值时,如果i和j大于len(s) - 1
if they are greater.len(s)-1
,则它们将减小为len(s)-1
。If i or j are omitted or如果i或j被省略或为None
, they become “end” values (which end depends on the sign of k).None
,它们将成为“结束”值(结束取决于k的符号)。Note, k cannot be zero.注意,k不能为零。If k is如果k为None
, it is treated like1
.None
,则将其视为1
。Concatenating immutable sequences always results in a new object.串联不可变序列总是会产生一个新对象。This means that building up a sequence by repeated concatenation will have a quadratic runtime cost in the total sequence length.这意味着通过重复串联构建序列将在整个序列长度中产生二次运行时成本。To get a linear runtime cost, you must switch to one of the alternatives below:要获得线性运行时成本,必须切换到以下备选方案之一:if concatenating如果串联str
objects, you can build a list and usestr.join()
at the end or else write to anio.StringIO
instance and retrieve its value when completestr
对象,则可以构建一个列表并在末尾使用str.join()
,或者写入io.StringIO
实例并在完成后检索其值if concatenating如果串联bytes
objects, you can similarly usebytes.join()
orio.BytesIO
, or you can do in-place concatenation with abytearray
object.bytes
对象,可以类似地使用bytes.join()
或io.BytesIO
,也可以使用bytearray
对象原地串联。bytearray
objects are mutable and have an efficient overallocation mechanism对象是可变的,并且具有高效的过度分配机制if concatenating如果串联tuple
objects, extend alist
insteadtuple
对象,请扩展list
for other types, investigate the relevant class documentation对于其他类型,请查看相关的类文档
Some sequence types (such as某些序列类型(如range
) only support item sequences that follow specific patterns, and hence don’t support sequence concatenation or repetition.range
)只支持遵循特定模式的项序列,因此不支持序列串联或重复。在s中找不到x时,index
raisesValueError
when x is not found in s.index
会引发ValueError
。Not all implementations support passing the additional arguments i and j.并非所有实现都支持传递附加参数i和j。These arguments allow efficient searching of subsections of the sequence.这些参数允许有效地搜索序列的子部分。Passing the extra arguments is roughly equivalent to using传递额外的参数大致相当于使用s[i:j].index(x)
, only without copying any data and with the returned index being relative to the start of the sequence rather than the start of the slice.s[i:j].index(x)
,仅在不复制任何数据的情况下,返回的索引是相对于序列的开始而不是片段的开始的。
Immutable Sequence Types不可变序列类型¶
The only operation that immutable sequence types generally implement that is not also implemented by mutable sequence types is support for the 不可变序列类型通常实现的唯一操作是对内置hash()
built-in.hash()
的支持,而可变序列类型也不实现该操作。
This support allows immutable sequences, such as 这种支持允许不可变序列(如tuple
instances, to be used as dict
keys and stored in set
and frozenset
instances.tuple
实例)用作dict
键,并存储在set
和frozenset
实例中。
Attempting to hash an immutable sequence that contains unhashable values will result in 试图散列包含不可破坏值的不可变序列将导致TypeError
.TypeError
。
Mutable Sequence Types可变序列类型¶
The operations in the following table are defined on mutable sequence types. 下表中的操作是在可变序列类型上定义的。The 提供collections.abc.MutableSequence
ABC is provided to make it easier to correctly implement these operations on custom sequence types.collections.abc.MutableSequence
抽象基类可以更轻松地在自定义序列类型上正确实现这些操作。
In the table s is an instance of a mutable sequence type, t is any iterable object and x is an arbitrary object that meets any type and value restrictions imposed by s (for example, 在表中,s是可变序列类型的实例,t是任意iterable对象,x是满足s施加的任何类型和值限制的任意对象(例如,bytearray
only accepts integers that meet the value restriction 0 <= x <= 255
).bytearray
只接受满足值限制0 <= x <= 255
的整数)。
|
|
|
---|---|---|
|
|
|
|
|
|
|
|
|
|
|
(1) |
|
|
|
|
|
|
|
|
(5) |
|
|
(5) |
|
|
|
|
|
(6) |
|
|
|
|
|
(2) |
|
|
(3) |
|
|
(4) |
Notes:
t must have the same length as the slice it is replacing.t的长度必须与其所替换的切片相同。The optional argument i defaults to可选参数i默认为-1
, so that by default the last item is removed and returned.-1
,因此默认情况下,最后一项被删除并返回。remove()
raisesValueError
when x is not found in s.remove()
在s中找不到x时引发ValueError
。The当反转大序列时,reverse()
method modifies the sequence in place for economy of space when reversing a large sequence.reverse()
方法原地修改序列以节省空间。To remind users that it operates by side effect, it does not return the reversed sequence.为了提醒用户它是通过副作用操作的,它不会返回相反的顺序。包含clear()
andcopy()
are included for consistency with the interfaces of mutable containers that don’t support slicing operations (such asdict
andset
).clear()
和copy()
是为了与不支持切片操作(如dict
和set
)的可变容器的接口保持一致。copy()
is not part of thecollections.abc.MutableSequence
ABC, but most concrete mutable sequence classes provide it.copy()
不是collections.abc.MutableSequence
抽象基类的一部分,但大多数具体的可变序列类都提供了它。New in version 3.3:版本3.3中新增:clear()
andcopy()
methods.clear()
和copy()
方法。The value n is an integer, or an object implementing值n是一个整数,或者是实现__index__()
.__index__()
的对象。Zero and negative values of n clear the sequence.n的零值和负值清除序列。Items in the sequence are not copied; they are referenced multiple times, as explained for未复制序列中的项目;它们被多次引用,如常见序列操作下的s * n
under Common Sequence Operations.s*n
所述。
Lists列表¶
Lists are mutable sequences, typically used to store collections of homogeneous items (where the precise degree of similarity will vary by application).列表是可变序列,通常用于存储同质项目的集合(其中精确的相似度将因应用程序而异)。
-
class
list
([iterable])¶ Lists may be constructed in several ways:列表可以通过多种方式构建:Using a pair of square brackets to denote the empty list:使用一对方括号表示空列表:[]
Using square brackets, separating items with commas:使用方括号,用逗号分隔项目:[a]
,[a, b, c]
[a]
、[a, b, c]
Using a list comprehension:使用列表推导:[x for x in iterable]
Using the type constructor:使用类型构造函数:list()
orlist(iterable)
list()
或list(iterable)
The constructor builds a list whose items are the same and in the same order as iterable’s items.构造函数构建一个列表,其项与iterable的项相同,且顺序相同。iterablemay be either a sequence, a container that supports iteration, or an iterator object.可以是序列、支持迭代的容器或迭代器对象。If iterable is already a list, a copy is made and returned, similar to如果iterable已经是一个列表,则创建并返回一个副本,类似于iterable[:]
.iterable[:]
。For example,例如,list('abc')
returns['a', 'b', 'c']
andlist( (1, 2, 3) )
returns[1, 2, 3]
.list('abc')
返回['a', 'b', 'c']
,而list( (1, 2, 3) )
返回[1, 2, 3]
。If no argument is given, the constructor creates a new empty list,如果没有给出参数,构造函数将创建一个新的空列表[]
.[]
。Many other operations also produce lists, including the许多其他操作也会生成列表,包括内置的sorted()
built-in.sorted()
。Lists implement all of the common and mutable sequence operations.列表实现所有常见的和可变的序列操作。Lists also provide the following additional method:列表还提供了以下附加方法:-
sort
(*, key=None, reverse=False)¶ This method sorts the list in place, using only此方法仅使用项与项之间的<
comparisons between items.<
比较对列表进行排序。Exceptions are not suppressed - if any comparison operations fail, the entire sort operation will fail (and the list will likely be left in a partially modified state).异常不会被抑制-如果任何比较操作失败,整个排序操作将失败(列表可能处于部分修改状态)。sort()
accepts two arguments that can only be passed by keyword (keyword-only arguments):接受只能通过关键字传递的两个参数(仅关键字参数):key specifies a function of one argument that is used to extract a comparison key from each list element (for example,key指定一个参数的函数,该参数用于从每个列表元素中提取比较键(例如,key=str.lower
).key=strlower
)。The key corresponding to each item in the list is calculated once and then used for the entire sorting process.列表中每个项目对应的键计算一次,然后用于整个排序过程。The default value of默认值None
means that list items are sorted directly without calculating a separate key value.None
表示直接对列表项进行排序,而不计算单独的键值。Thefunctools.cmp_to_key()
utility is available to convert a 2.x style cmp function to a key function.functools.cmp_to_key()
实用程序可用于将2.x样式的cmp函数转换为key函数。reverse is a boolean value.reverse是布尔值。If set to如果设置为True
, then the list elements are sorted as if each comparison were reversed.True
,则列表元素将被排序,就像每个比较都被反转一样。This method modifies the sequence in place for economy of space when sorting a large sequence.此方法在排序大型序列时修改现有序列以节省空间。To remind users that it operates by side effect, it does not return the sorted sequence (use为了提醒用户它的操作有副作用,它不会返回排序序列(使用sorted()
to explicitly request a new sorted list instance).sorted()
显式请求一个新的排序列表实例)。Thesort()
method is guaranteed to be stable.sort()
方法保证是稳定的。A sort is stable if it guarantees not to change the relative order of elements that compare equal — this is helpful for sorting in multiple passes (for example, sort by department, then by salary grade).如果排序保证不会更改比较相等的元素的相对顺序,则排序是稳定的-这有助于在多个过程中进行排序(例如,按部门排序,然后按薪资等级排序)。For sorting examples and a brief sorting tutorial, see Sorting HOW TO.有关排序示例和简短的排序教程,请参阅排序方式。CPython implementation detail: While a list is being sorted, the effect of attempting to mutate, or even inspect, the list is undefined.CPython实现细节:当列表被排序时,试图改变甚至检查列表的效果是未定义的。The C implementation of Python makes the list appear empty for the duration, and raisesPython的C实现使列表在整个时间段内显示为空,如果它能够检测到列表在排序过程中发生了变化,则会引发ValueError
if it can detect that the list has been mutated during a sort.ValueError
。
Tuples多元组¶
Tuples are immutable sequences, typically used to store collections of heterogeneous data (such as the 2-tuples produced by the 元组是不可变的序列,通常用于存储异构数据的集合(例如内置的enumerate()
built-in). enumerate()
生成的2元组)。Tuples are also used for cases where an immutable sequence of homogeneous data is needed (such as allowing storage in a 元组还用于需要不可变的同质数据序列的情况(例如允许在set
or dict
instance).set
或dict
实例中存储)。
-
class
tuple
([iterable])¶ Tuples may be constructed in a number of ways:元组可以通过多种方式构造:Using a pair of parentheses to denote the empty tuple:使用一对括号表示空元组:()
()
Using a trailing comma for a singleton tuple:对单元组使用尾随逗号:a,
or或(a,)
Separating items with commas:用逗号分隔项目:a, b, c
or或(a, b, c)
Using the使用内置的tuple()
built-in:tuple()
ortuple(iterable)
tuple()
:tuple()
或tuple(iterable)
The constructor builds a tuple whose items are the same and in the same order as iterable’s items.构造函数构建一个元组,其项与iterable的项相同且顺序相同。iterable may be either a sequence, a container that supports iteration, or an iterator object.iterable可以是序列、支持迭代的容器或迭代器对象。If iterable is already a tuple, it is returned unchanged.如果iterable已经是一个元组,则返回时将保持不变。For example,例如,tuple('abc')
returns('a', 'b', 'c')
andtuple( [1, 2, 3] )
returns(1, 2, 3)
.tuple('abc')
返回('a', 'b', 'c')
,tuple( [1, 2, 3] )
返回(1, 2, 3)
。If no argument is given, the constructor creates a new empty tuple,如果没有给出参数,构造函数将创建一个新的空元组,()
.()
。Note that it is actually the comma which makes a tuple, not the parentheses.请注意,实际上是逗号构成元组,而不是括号。The parentheses are optional, except in the empty tuple case, or when they are needed to avoid syntactic ambiguity.括号是可选的,除非是在空元组情况下,或者需要括号来避免语法歧义。For example,例如,f(a, b, c)
is a function call with three arguments, whilef((a, b, c))
is a function call with a 3-tuple as the sole argument.f(a, b, c)
是一个具有三个参数的函数调用,而f((a, b, c))
是一个具有三元组作为唯一参数的函数调用。Tuples implement all of the common sequence operations.元组实现所有常见的序列操作。
For heterogeneous collections of data where access by name is clearer than access by index, 对于按名称访问比按索引访问更清晰的异构数据集合,collections.namedtuple()
may be a more appropriate choice than a simple tuple object.collections.namedtuple()
可能比简单的tuple对象更合适。
Ranges范围¶
The range
type represents an immutable sequence of numbers and is commonly used for looping a specific number of times in for
loops.range
类型表示一个不可变的数字序列,通常用于在for
循环中循环特定次数。
-
class
range
(stop)¶ -
class
range
(start, stop[, step]) The arguments to the range constructor must be integers (either built-in范围构造函数的参数必须是整数(内置int
or any object that implements the__index__()
special method).int
或任何实现__index__()
特殊方法的对象)。If the step argument is omitted, it defaults to如果省略step参数,则默认为1
.1
。If the start argument is omitted, it defaults to如果省略start参数,则默认为0
.0
。If step is zero,如果step为零,则会引发ValueError
is raised.ValueError
。For a positive step, the contents of a range对于正step,范围r
are determined by the formular[i] = start + step*i
wherei >= 0
andr[i] < stop
.r
的内容由公式r[i] = start + step*i
确定,其中i >= 0
、r[i] < stop
。For a negative step, the contents of the range are still determined by the formula对于负step,范围的内容仍然由公式r[i] = start + step*i
, but the constraints arei >= 0
andr[i] > stop
.r[i] = start + step*i
确定,但约束条件是i >= 0
和r[i] > stop
。A range object will be empty if如果r[0]
does not meet the value constraint.r[0]
不满足值约束,则范围对象将为空。Ranges do support negative indices, but these are interpreted as indexing from the end of the sequence determined by the positive indices.范围确实支持负指数,但这些被解释为从正指数确定的序列末尾开始的索引。Ranges containing absolute values larger than允许包含大于sys.maxsize
are permitted but some features (such aslen()
) may raiseOverflowError
.sys.maxsize
的绝对值的范围,但某些功能(如len()
)可能会引发OverflowError
。Range examples:范围示例:>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(range(1, 11))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> list(range(0, 30, 5))
[0, 5, 10, 15, 20, 25]
>>> list(range(0, 10, 3))
[0, 3, 6, 9]
>>> list(range(0, -10, -1))
[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
>>> list(range(0))
[]
>>> list(range(1, 0))
[]Ranges implement all of the common sequence operations except concatenation and repetition (due to the fact that range objects can only represent sequences that follow a strict pattern and repetition and concatenation will usually violate that pattern).范围实现除串联和重复之外的所有常见的序列操作(因为范围对象只能表示遵循严格模式的序列,而重复和串联通常会违反该模式)。-
start
¶ The value of the start parameter (orstart参数的值(如果未提供该参数,则为0
if the parameter was not supplied)0
)
-
stop
¶ The value of the stop parameterstop参数的值
-
step
¶ The value of the step parameter (orstep参数的值(如果未提供参数,则为1
if the parameter was not supplied)1
)
-
The advantage of the 与常规range
type over a regular list
or tuple
is that a range
object will always take the same (small) amount of memory, no matter the size of the range it represents (as it only stores the start
, stop
and step
values, calculating individual items and subranges as needed).list
或tuple
相比,range
类型的优势在于,范围对象始终会占用相同(较小)的内存量,无论它所表示的范围大小如何(因为它只存储start
、stop
和step
值,根据需要计算单个项和子范围)。
Range objects implement the 范围对象实现collections.abc.Sequence
ABC, and provide features such as containment tests, element index lookup, slicing and support for negative indices (see Sequence Types — list, tuple, range):collections.abc.Sequence
抽象基类,并提供包含测试、元素索引查找、切片和负索引支持等功能(请参见序列类型:列表、元组、范围):
>>> r = range(0, 20, 2)
>>> r
range(0, 20, 2)
>>> 11 in r
False
>>> 10 in r
True
>>> r.index(10)
5
>>> r[5]
10
>>> r[:5]
range(0, 10, 2)
>>> r[-1]
18
Testing range objects for equality with 使用==
and !=
compares them as sequences. ==
和!=
测试范围对象的等同性,将把它们作为序列进行比较。That is, two range objects are considered equal if they represent the same sequence of values. 也就是说,如果两个范围对象表示相同的值序列,则认为它们相等。(Note that two range objects that compare equal might have different (请注意,比较相等的两个范围对象可能具有不同的start
, stop
and step
attributes, for example range(0) == range(2, 1, 3)
or range(0, 3, 2) == range(0, 4, 2)
.)start
、stop
和step
属性,例如range(0) == range(2, 1, 3)
或range(0, 3, 2) == range(0, 4, 2)
。)
Changed in version 3.2: 版本3.2中更改:Implement the Sequence ABC. 实现Sequence抽象基类。Support slicing and negative indices. 支持切片和负索引。Test 在固定时间内测试int
objects for membership in constant time instead of iterating through all items.int
对象的成员身份,而不是遍历所有项。
Changed in version 3.3: 版本3.3中更改:Define ‘==’ and ‘!=’ to compare range objects based on the sequence of values they define (instead of comparing based on object identity).定义“==”和“!=”根据范围对象定义的值序列比较范围对象(而不是根据对象标识进行比较)。
See also
The linspace recipe shows how to implement a lazy version of range suitable for floating point applications.linspace配方显示了如何实现适用于浮点应用程序的范围的延迟版本。
Text Sequence Type — 文本序列类型:str
¶
Textual data in Python is handled with Python中的文本数据是用str
objects, or strings. str
对象或字符串处理的。Strings are immutable sequences of Unicode code points. 字符串是Unicode代码点的不可变序列。String literals are written in a variety of ways:字符串文字有多种编写方式:
Single quotes:单引号:'allows embedded "double" quotes'
Double quotes:双引号:"allows embedded 'single' quotes"
Triple quoted:三引号:'''Three single quotes'''
,"""Three double quotes"""
Triple quoted strings may span multiple lines - all associated whitespace will be included in the string literal.三引号字符串可能跨越多行-所有关联的空格将包含在字符串文字中。
String literals that are part of a single expression and have only whitespace between them will be implicitly converted to a single string literal. 作为单个表达式的一部分并且它们之间只有空格的字符串文字将被隐式转换为单个字符串文字。That is, 也就是说:("spam " "eggs") == "spam eggs"
.("spam " "eggs") == "spam eggs"
。
See String and Bytes literals for more about the various forms of string literal, including supported escape sequences, and the 有关字符串文字的各种形式的更多信息,请参阅字符串和字节文字,包括支持的转义序列,以及禁用大多数转义序列处理的r
(“raw”) prefix that disables most escape sequence processing.r
(“raw”)前缀。
Strings may also be created from other objects using the 也可以使用str
constructor.str
构造函数从其他对象创建字符串。
Since there is no separate “character” type, indexing a string produces strings of length 1. 由于没有单独的“字符”类型,因此索引字符串会生成长度为1的字符串。That is, for a non-empty string s, 也就是说,对于非空字符串s,s[0] == s[0:1]
.s[0]==s[0:1]
。
There is also no mutable string type, but 也没有可变的字符串类型,但是str.join()
or io.StringIO
can be used to efficiently construct strings from multiple fragments.str.join()
或io.StringIO
可以用来有效地从多个片段构造字符串。
Changed in version 3.3: 版本3.3中更改:For backwards compatibility with the Python 2 series, the 为了向后兼容Python 2系列,再次允许在字符串文字上使用u
prefix is once again permitted on string literals. u
前缀。It has no effect on the meaning of string literals and cannot be combined with the 它对字符串文字的含义没有影响,并且不能与r
prefix.r
前缀组合。
-
class
str
(object='')¶ -
class
str
(object=b'', encoding='utf-8', errors='strict') Return a string version of object.返回object的字符串版本。If object is not provided, returns the empty string.如果未提供object,则返回空字符串。Otherwise, the behavior of否则,str()
depends on whether encoding or errors is given, as follows.str()
的行为取决于给定的是encoding还是errors,如下所示。If neither encoding nor errors is given,如果既没有给出encoding也没有给出errors,str(object)
returnstype(object).__str__(object)
, which is the “informal” or nicely printable string representation of object.str(object)
将返回type(object).__str__(object)
,这是object的“非正式”或可良好打印的字符串表示形式。For string objects, this is the string itself.对于字符串对象,这是字符串本身。If object does not have a如果object没有__str__()
method, thenstr()
falls back to returningrepr(object)
.__str__()
方法,则str()
回退到返回repr(object)
。If at least one of encoding or errors is given, object should be a bytes-like object (e.g.如果至少给出了一个encoding或error,则object应该是类似字节的对象(例如bytes
orbytearray
). In this case, if object is abytes
(orbytearray
) object, thenstr(bytes, encoding, errors)
is equivalent tobytes.decode(encoding, errors)
.bytes
或bytearray
)。在这种情况下,如果object是bytes
(或bytearray
)对象,则str(bytes, encoding, errors)
等效于bytes.decode(encoding, errors)
。Otherwise, the bytes object underlying the buffer object is obtained before calling否则,将在调用bytes.decode()
.bytes.decode()
之前获取缓冲区对象下面的字节对象。See Binary Sequence Types — bytes, bytearray, memoryview and Buffer Protocol for information on buffer objects.有关缓冲区对象的信息,请参阅二进制序列类型:字节、字节数组、内存视图和缓冲区协议。Passing a在没有encoding或errors参数的情况下将bytes
object tostr()
without the encoding or errors arguments falls under the first case of returning the informal string representation (see also the-b
command-line option to Python).bytes
对象传递给str()
属于第一种返回非正式字符串表示的情况(另请参见Python的-b
命令行选项)。For example:例如:>>> str(b'Zoot!')
"b'Zoot!'"For more information on the有关str
class and its methods, see Text Sequence Type — str and the String Methods section below.str
类及其方法的更多信息,请参阅下面的文本序列类型:str和字符串方法部分。To output formatted strings, see the Formatted string literals and Format String Syntax sections.要输出格式化字符串,请参阅格式化字符串文字和格式化字符串语法部分。In addition, see the Text Processing Services section.此外,请参阅文本处理服务部分。
String Methods字符串方法¶
Strings implement all of the common sequence operations, along with the additional methods described below.字符串实现所有常见的序列操作,以及下面描述的其他方法。
Strings also support two styles of string formatting, one providing a large degree of flexibility and customization (see 字符串还支持两种样式的字符串格式,一种提供了很大程度的灵活性和自定义(请参见str.format()
, Format String Syntax and Custom String Formatting) and the other based on C printf
style formatting that handles a narrower range of types and is slightly harder to use correctly, but is often faster for the cases it can handle (printf-style String Formatting).str.format()
,格式字符串语法和自定义字符串格式),另一种基于C printf
样式的格式,它处理的类型范围较窄,更难正确使用,但对于它可以处理的情况(printf样式的字符串格式),通常速度更快。
The Text Processing Services section of the standard library covers a number of other modules that provide various text related utilities (including regular expression support in the 标准库的文本处理服务部分包括许多其他模块,这些模块提供各种与文本相关的实用程序(包括re
module).re
模块中的正则表达式支持)。
-
str.
capitalize
()¶ Return a copy of the string with its first character capitalized and the rest lowercased.返回字符串的副本,第一个字符大写,其余字符小写。Changed in version 3.8:版本3.8中更改:The first character is now put into titlecase rather than uppercase.第一个字符现在被放在titlecase中,而不是大写。This means that characters like digraphs will only have their first letter capitalized, instead of the full character.这意味着像有向图这样的字符只有第一个字母大写,而不是完整的字符。
-
str.
casefold
()¶ Return a casefolded copy of the string.返回字符串的折叠副本。Casefolded strings may be used for caseless matching.折叠的字符串可用于无壳匹配。Casefolding is similar to lowercasing but more aggressive because it is intended to remove all case distinctions in a string.大小写折叠类似于小写,但更具攻击性,因为它旨在消除字符串中的所有大小写区别。For example, the German lowercase letter例如,德语小写字母'ß'
is equivalent to"ss"
.'ß'
相当于"ss"
。Since it is already lowercase,因为它已经是小写的,所以lower()
would do nothing to'ß'
;casefold()
converts it to"ss"
.lower()
对'ß'
没有任何作用;casefold()
将其转换为"ss"
。The casefolding algorithm is described in section 3.13 of the Unicode Standard.Unicode标准的第3.13节描述了案例折叠算法。New in version 3.3.版本3.3中新增。
-
str.
center
(width[, fillchar])¶ Return centered in a string of length width.返回以长度width的字符串为中心。Padding is done using the specified fillchar (default is an ASCII space).填充是使用指定的fillchar完成的(默认为ASCII空格)。The original string is returned if width is less than or equal to如果width小于或等于len(s)
.len(s)
,则返回原始字符串。
-
str.
count
(sub[, start[, end]])¶ Return the number of non-overlapping occurrences of substring sub in the range [start, end].返回[start, end]范围内子字符串sub的非重叠出现次数。Optional arguments start and end are interpreted as in slice notation.可选参数start和end被解释为切片表示法。
-
str.
encode
(encoding='utf-8', errors='strict')¶ Return an encoded version of the string as a bytes object.以字节对象的形式返回字符串的编码版本。Default encoding is默认编码为'utf-8'
.'utf-8'
。errors may be given to set a different error handling scheme.可能会给出errors以设置不同的错误处理方案。The default for errors iserrors的默认值为'strict'
, meaning that encoding errors raise aUnicodeError
.'strict'
,这意味着编码错误会引发UnicodeError
。Other possible values are其他可能的值包括'ignore'
,'replace'
,'xmlcharrefreplace'
,'backslashreplace'
and any other name registered viacodecs.register_error()
, see section Error Handlers.'ignore'
、'replace'
、'xmlcharrefreplace'
、'backslashreplace'
和通过codecs.register_error()
注册的任何其他名称,请参阅错误处理程序一节。For a list of possible encodings, see section Standard Encodings.有关可能的编码列表,请参阅标准编码一节。By default, the errors argument is not checked for best performances, but only used at the first encoding error.默认情况下,不会检查errors参数以获得最佳性能,而只在第一个编码错误时使用。Enable the Python Development Mode, or use a debug build to check errors.启用Python开发模式,或使用调试构建来检查错误。Changed in version 3.1:版本3.1中更改:Support for keyword arguments added.添加了对关键字参数的支持。Changed in version 3.9:版本3.9中更改:The errors is now checked in development mode and in debug mode.现在在开发模式和调试模式中检查errors。
-
str.
endswith
(suffix[, start[, end]])¶ Return如果字符串以指定后缀结尾,则返回True
if the string ends with the specified suffix, otherwise returnFalse
.True
,否则返回False
。suffix can also be a tuple of suffixes to look for.suffix也可以是要查找的后缀元组。With optional start, test beginning at that position.使用可选的start,从该位置开始测试。With optional end, stop comparing at that position.使用可选的end,在该位置停止比较。
-
str.
expandtabs
(tabsize=8)¶ Return a copy of the string where all tab characters are replaced by one or more spaces, depending on the current column and the given tab size.根据当前列和给定的制表符大小,返回所有制表符都被一个或多个空格替换的字符串副本。Tab positions occur every tabsize characters (default is 8, giving tab positions at columns 0, 8, 16 and so on).每个tabsize字符都会出现制表符位置(默认值为8,在第0、8、16列等处给出制表符位置)。To expand the string, the current column is set to zero and the string is examined character by character.要展开字符串,请将当前列设置为零,并逐个字符检查字符串。If the character is a tab (如果字符是制表符(\t
), one or more space characters are inserted in the result until the current column is equal to the next tab position.\t
),将在结果中插入一个或多个空格字符,直到当前列等于下一个制表符位置。(The tab character itself is not copied.)(不会复制制表符本身。)If the character is a newline (如果字符是换行符(\n
) or return (\r
), it is copied and the current column is reset to zero.\n
)或返回符(\r
),则复制该字符,并将当前列重置为零。Any other character is copied unchanged and the current column is incremented by one regardless of how the character is represented when printed.任何其他字符都会原封不动地复制,并且当前列将递增1,无论打印时如何表示该字符。>>> '01\t012\t0123\t01234'.expandtabs()
'01 012 0123 01234'
>>> '01\t012\t0123\t01234'.expandtabs(4)
'01 012 0123 01234'
-
str.
find
(sub[, start[, end]])¶ Return the lowest index in the string where substring sub is found within the slice返回在切片s[start:end]
.s[start:end]
中找到子字符串sub的字符串中的最低索引。Optional arguments start and end are interpreted as in slice notation.可选参数start和end被解释为切片表示法。Return如果未找到sub,则返回-1
if sub is not found.-1
。
-
str.
format
(*args, **kwargs)¶ Perform a string formatting operation.执行字符串格式化操作。The string on which this method is called can contain literal text or replacement fields delimited by braces调用此方法的字符串可以包含由大括号{}
.{}
分隔的文本或替换字段。Each replacement field contains either the numeric index of a positional argument, or the name of a keyword argument.每个替换字段包含位置参数的数字索引或关键字参数的名称。Returns a copy of the string where each replacement field is replaced with the string value of the corresponding argument.返回字符串的副本,其中每个替换字段都替换为相应参数的字符串值。>>> "The sum of 1 + 2 is {0}".format(1+2)
'The sum of 1 + 2 is 3'See Format String Syntax for a description of the various formatting options that can be specified in format strings.有关可以在格式字符串中指定的各种格式选项的说明,请参阅格式字符串语法。Note
When formatting a number (当使用int
,float
,complex
,decimal.Decimal
and subclasses) with then
type (ex:'{:n}'.format(1234)
), the function temporarily sets theLC_CTYPE
locale to theLC_NUMERIC
locale to decodedecimal_point
andthousands_sep
fields oflocaleconv()
if they are non-ASCII or longer than 1 byte, and theLC_NUMERIC
locale is different than theLC_CTYPE
locale.n
类型(例如:'{:n}'.format(1234)
)格式化数字(int
、float
、complex
、decimal.Decimal
和子类)时,函数会临时将LC_CTYPE
区域设置为LC_NUMERIC
区域设置,以解码localeconv()
的decimal_point
和thousands_sep
字段(如果它们不是ASCII或长于1字节),并且LC_NUMERIC
区域设置不同于LC_CTYPE
区域设置。This temporary change affects other threads.此临时更改会影响其他线程。Changed in version 3.7:版本3.7中更改:When formatting a number with the使用n
type, the function sets temporarily theLC_CTYPE
locale to theLC_NUMERIC
locale in some cases.n
类型格式化数字时,函数会在某些情况下将LC_CTYPE
区域设置临时设置为LC_NUMERIC
区域设置。
-
str.
format_map
(mapping)¶ Similar to与str.format(**mapping)
, except thatmapping
is used directly and not copied to adict
.str.format(**mapping)
类似,只是mapping
是直接使用的,而不是复制到dict
。This is useful if for example例如,如果mapping
is a dict subclass:mapping
是dict子类,则这很有用:>>> class Default(dict):
... def __missing__(self, key):
... return key
...
>>> '{name} was born in {country}'.format_map(Default(name='Guido'))
'Guido was born in country'New in version 3.2.版本3.2中新增。
-
str.
index
(sub[, start[, end]])¶ Like与find()
, but raiseValueError
when the substring is not found.find()
类似,但在未找到子字符串时引发ValueError
。
-
str.
isalnum
()¶ Return如果字符串中的所有字符都是字母数字且至少有一个字符,则返回True
if all characters in the string are alphanumeric and there is at least one character,False
otherwise.True
,否则返回False
。A character如果以下任一项返回c
is alphanumeric if one of the following returnsTrue
:c.isalpha()
,c.isdecimal()
,c.isdigit()
, orc.isnumeric()
.True
,则字符c
为字母数字:c.isalpha()
、c.isdecimal()
、c.isdigit()
或c.isnumeric()
。
-
str.
isalpha
()¶ Return如果字符串中的所有字符都是字母,并且至少有一个字符,则返回True
if all characters in the string are alphabetic and there is at least one character,False
otherwise.True
,否则返回False
。Alphabetic characters are those characters defined in the Unicode character database as “Letter”, i.e., those with general category property being one of “Lm”, “Lt”, “Lu”, “Ll”, or “Lo”.字母字符是在Unicode字符数据库中定义为“字母”的字符,即具有“Lm”、“Lt”、“Lu”、“Ll”或“Lo”之一的常规类别属性的字符。Note that this is different from the “Alphabetic” property defined in the Unicode Standard.请注意,这与Unicode标准中定义的“字母”属性不同。
-
str.
isascii
()¶ Return如果字符串为空或字符串中的所有字符均为ASCII,则返回True
if the string is empty or all characters in the string are ASCII,False
otherwise.True
,否则返回False
。ASCII characters have code points in the range U+0000-U+007F.ASCII字符的代码点范围为U+0000-U+007F。New in version 3.7.版本3.7中新增。
-
str.
isdecimal
()¶ Return如果字符串中的所有字符都是十进制字符且至少有一个字符,则返回True
if all characters in the string are decimal characters and there is at least one character,False
otherwise.True
,否则返回False
。Decimal characters are those that can be used to form numbers in base 10, e.g. U+0660, ARABIC-INDIC DIGIT ZERO.十进制字符是那些可以用来形成以10为基数的数字的字符,例如U+0660、阿拉伯-印度数字零。Formally a decimal character is a character in the Unicode General Category “Nd”.形式上,十进制字符是Unicode通用类别“Nd”中的字符。
-
str.
isdigit
()¶ Return如果字符串中的所有字符都是数字并且至少有一个字符,则返回True
if all characters in the string are digits and there is at least one character,False
otherwise.True
,否则返回False
。Digits include decimal characters and digits that need special handling, such as the compatibility superscript digits.数字包括十进制字符和需要特殊处理的数字,例如兼容性上标数字。This covers digits which cannot be used to form numbers in base 10, like the Kharosthi numbers.这包括不能用于构成以10为基数的数字的数字,如Kharosthi数字。Formally, a digit is a character that has the property value Numeric_Type=Digit or Numeric_Type=Decimal.形式上,数字是一个属性值Numeric_Type=digit或Numeric_Type=Decimal的字符。
-
str.
isidentifier
()¶ Return根据语言定义、标识符和关键字这一节,如果字符串是有效标识符,则返回True
if the string is a valid identifier according to the language definition, section Identifiers and keywords.True
。Call调用keyword.iskeyword()
to test whether strings
is a reserved identifier, such asdef
andclass
.keyword.iskeyword()
以测试字符串s
是否为保留标识符,如def
和class
。Example:
>>> from keyword import iskeyword
>>> 'hello'.isidentifier(), iskeyword('hello')
(True, False)
>>> 'def'.isidentifier(), iskeyword('def')
(True, True)
-
str.
islower
()¶ Return如果字符串中所有大小写字符4都是小写且至少有一个大小写字符,则返回True
if all cased characters 4 in the string are lowercase and there is at least one cased character,False
otherwise.True
,否则返回False
。
-
str.
isnumeric
()¶ Return如果字符串中的所有字符都是数字字符,并且至少有一个字符,则返回True
if all characters in the string are numeric characters, and there is at least one character,False
otherwise.True
,否则返回False
。Numeric characters include digit characters, and all characters that have the Unicode numeric value property, e.g. U+2155, VULGAR FRACTION ONE FIFTH.数字字符包括数字字符,以及所有具有Unicode数值属性的字符,例如U+2155,普通分数五分之一。Formally, numeric characters are those with the property value Numeric_Type=Digit, Numeric_Type=Decimal or Numeric_Type=Numeric.形式上,数字字符是那些属性值numeric_Type=Digit、numeric_Type=Decimal或numeric_Type=numeric的字符。
-
str.
isprintable
()¶ Return如果字符串中的所有字符都可打印或字符串为空,则返回True
if all characters in the string are printable or the string is empty,False
otherwise.True
,否则返回False
。Nonprintable characters are those characters defined in the Unicode character database as “Other” or “Separator”, excepting the ASCII space (0x20) which is considered printable.不可打印字符是指在Unicode字符数据库中定义为“其他”或“分隔符”的字符,ASCII空格(0x20)除外,它被认为是可打印的。(Note that printable characters in this context are those which should not be escaped when(请注意,此上下文中的可打印字符是在字符串上调用repr()
is invoked on a string.repr()
时不应转义的字符。It has no bearing on the handling of strings written to它与处理写入sys.stdout
orsys.stderr
.)sys.stdout
或sys.stderr
的字符串无关。)
-
str.
isspace
()¶ Return如果字符串中只有空格字符且至少有一个字符,则返回True
if there are only whitespace characters in the string and there is at least one character,False
otherwise.True
,否则返回False
。A character is whitespace if in the Unicode character database (see如果在Unicode字符数据库(请参见unicodedata
), either its general category isZs
(“Separator, space”), or its bidirectional class is one ofWS
,B
, orS
.unicodedata
)中,字符的一般类别为Zs
(“分隔符,空格”),或者其双向类为WS
、B
或S
之一,则字符为whitespace。
-
str.
istitle
()¶ Return如果字符串是基于标题的字符串,并且至少有一个字符,例如大写字符只能跟在无标题字符后面,小写字符只能跟在大小写字符后面,则返回True
if the string is a titlecased string and there is at least one character, for example uppercase characters may only follow uncased characters and lowercase characters only cased ones.True
。Return否则返回False
otherwise.False
。
-
str.
isupper
()¶ Return如果字符串中所有大小写字符4都是大写且至少有一个大小写字符,则返回True
if all cased characters 4 in the string are uppercase and there is at least one cased character,False
otherwise.True
,否则返回False
。>>> 'BANANA'.isupper()
True
>>> 'banana'.isupper()
False
>>> 'baNana'.isupper()
False
>>> ' '.isupper()
False
-
str.
join
(iterable)¶ Return a string which is the concatenation of the strings in iterable.返回一个字符串,该字符串是iterable中字符串的串联。A如果iterable中有任何非字符串值,包括TypeError
will be raised if there are any non-string values in iterable, includingbytes
objects.bytes
对象,则会引发TypeError
。The separator between elements is the string providing this method.元素之间的分隔符是提供此方法的字符串。
-
str.
ljust
(width[, fillchar])¶ Return the string left justified in a string of length width.返回长度width字符串中左对齐的字符串。Padding is done using the specified fillchar (default is an ASCII space).填充是使用指定的fillchar完成的(默认为ASCII空格)。The original string is returned if width is less than or equal to如果width小于或等于len(s)
.len(s)
,则返回原始字符串。
-
str.
lower
()¶ Return a copy of the string with all the cased characters 4 converted to lowercase.返回字符串的副本,其中所有大小写字符4都转换为小写。The lowercasing algorithm used is described in section 3.13 of the Unicode Standard.Unicode标准第3.13节描述了使用的小写算法。
-
str.
lstrip
([chars])¶ Return a copy of the string with leading characters removed.返回删除前导字符的字符串副本。The chars argument is a string specifying the set of characters to be removed.chars参数是一个字符串,指定要删除的字符集。If omitted or如果省略或为None
, the chars argument defaults to removing whitespace.None
,chars参数默认为删除空白。The chars argument is not a prefix; rather, all combinations of its values are stripped:chars参数不是前缀;相反,其值的所有组合都被剥离:>>> ' spacious '.lstrip()
'spacious '
>>> 'www.example.com'.lstrip('cmowz.')
'example.com'See有关将删除单个前缀字符串而不是所有字符集的方法,请参阅str.removeprefix()
for a method that will remove a single prefix string rather than all of a set of characters.str.removeprefix()
。For example:例如:>>> 'Arthur: three!'.lstrip('Arthur: ')
'ee!'
>>> 'Arthur: three!'.removeprefix('Arthur: ')
'three!'
-
static
str.
maketrans
(x[, y[, z]])¶ This static method returns a translation table usable for此静态方法返回可用于str.translate()
.str.translate()
的转换表。If there is only one argument, it must be a dictionary mapping Unicode ordinals (integers) or characters (strings of length 1) to Unicode ordinals, strings (of arbitrary lengths) or如果只有一个参数,则必须是将Unicode序号(整数)或字符(长度为1的字符串)映射到Unicode序号、字符串(任意长度)或None
.None
的字典。Character keys will then be converted to ordinals.然后,字符键将转换为序号。If there are two arguments, they must be strings of equal length, and in the resulting dictionary, each character in x will be mapped to the character at the same position in y.如果有两个参数,则它们必须是长度相等的字符串,并且在生成的字典中,x中的每个字符都将映射到y中相同位置的字符。If there is a third argument, it must be a string, whose characters will be mapped to如果有第三个参数,则必须是字符串,其字符将在结果中映射为None
in the result.None
。
-
str.
partition
(sep)¶ Split the string at the first occurrence of sep, and return a 3-tuple containing the part before the separator, the separator itself, and the part after the separator.在sep第一次出现时拆分字符串,并返回一个3元组,其中包含分隔符之前的部分、分隔符本身以及分隔符之后的部分。If the separator is not found, return a 3-tuple containing the string itself, followed by two empty strings.如果未找到分隔符,则返回一个包含字符串本身的3元组,后跟两个空字符串。
-
str.
removeprefix
(prefix, /)¶ If the string starts with the prefix string, return如果字符串以prefix字符串开头,则返回string[len(prefix):]
.string[len(prefix):]
。Otherwise, return a copy of the original string:否则,返回原始字符串的副本:>>> 'TestHook'.removeprefix('Test')
'Hook'
>>> 'BaseTestCase'.removeprefix('Test')
'BaseTestCase'New in version 3.9.版本3.9中新增。
-
str.
removesuffix
(suffix, /)¶ If the string ends with the suffix string and that suffix is not empty, return如果字符串以suffix字符串结尾,并且该suffix不是空的,则返回string[:-len(suffix)]
.string[:-len(suffix)]
。Otherwise, return a copy of the original string:否则,返回原始字符串的副本:>>> 'MiscTests'.removesuffix('Tests')
'Misc'
>>> 'TmpDirMixin'.removesuffix('Tests')
'TmpDirMixin'New in version 3.9.版本3.9中新增。
-
str.
replace
(old, new[, count])¶ Return a copy of the string with all occurrences of substring old replaced by new.返回字符串的副本,其中所有出现的子字符串old替换为new。If the optional argument count is given, only the first count occurrences are replaced.如果给定了可选的参数count,则只替换第一次出现的count。
-
str.
rfind
(sub[, start[, end]])¶ Return the highest index in the string where substring sub is found, such that sub is contained within返回找到子字符串sub的字符串中的最高索引,以便sub包含在s[start:end]
.s[start:end]
中。Optional arguments start and end are interpreted as in slice notation.可选参数start和end被解释为切片表示法。Return故障时返回-1
on failure.-1
。
-
str.
rindex
(sub[, start[, end]])¶ Like与rfind()
but raisesValueError
when the substring sub is not found.rfind()
类似,但在找不到子字符串sub时引发ValueError
。
-
str.
rjust
(width[, fillchar])¶ Return the string right justified in a string of length width.返回长度width字符串中右对齐的字符串。Padding is done using the specified fillchar (default is an ASCII space).填充是使用指定的fillchar完成的(默认为ASCII空格)。The original string is returned if width is less than or equal to如果width小于或等于len(s)
.len(s)
,则返回原始字符串。
-
str.
rpartition
(sep)¶ Split the string at the last occurrence of sep, and return a 3-tuple containing the part before the separator, the separator itself, and the part after the separator.在sep的最后一次出现时拆分字符串,并返回一个3元组,其中包含分隔符之前的部分、分隔符本身以及分隔符之后的部分。If the separator is not found, return a 3-tuple containing two empty strings, followed by the string itself.如果未找到分隔符,则返回一个包含两个空字符串的3元组,后跟字符串本身。
-
str.
rsplit
(sep=None, maxsplit=- 1)¶ Return a list of the words in the string, using sep as the delimiter string.使用sep作为分隔符字符串,返回字符串中的单词列表。If maxsplit is given, at most maxsplit splits are done, the rightmost ones.如果给定maxsplit,则最多执行maxsplit拆分,最右边的拆分。If sep is not specified or如果未指定sep或指定为None
, any whitespace string is a separator.None
,则任何空白字符串都是分隔符。Except for splitting from the right,除了从右侧拆分之外,rsplit()
behaves likesplit()
which is described in detail below.rsplit()
的行为类似于split()
,下面将对其进行详细描述。
-
str.
rstrip
([chars])¶ Return a copy of the string with trailing characters removed.返回删除尾随字符的字符串副本。The chars argument is a string specifying the set of characters to be removed.chars参数是一个字符串,指定要删除的字符集。If omitted or如果省略或为None
, the chars argument defaults to removing whitespace.None
,chars参数默认为删除空白。The chars argument is not a suffix; rather, all combinations of its values are stripped:chars参数不是后缀;相反,其值的所有组合都被剥离:>>> ' spacious '.rstrip()
' spacious'
>>> 'mississippi'.rstrip('ipz')
'mississ'See请参阅str.removesuffix()
for a method that will remove a single suffix string rather than all of a set of characters.str.removesuffix()
,了解将删除单个后缀字符串而不是所有字符集的方法。For example:例如:>>> 'Monty Python'.rstrip(' Python')
'M'
>>> 'Monty Python'.removesuffix(' Python')
'Monty'
-
str.
split
(sep=None, maxsplit=- 1)¶ Return a list of the words in the string, using sep as the delimiter string.使用sep作为分隔符字符串,返回字符串中的单词列表。If maxsplit is given, at most maxsplit splits are done (thus, the list will have at most如果给定maxsplit,则最多完成maxsplit拆分(因此,列表最多包含maxsplit+1
elements).maxsplit+1
个元素)。If maxsplit is not specified or如果未指定maxsplit或-1
, then there is no limit on the number of splits (all possible splits are made).-1
,则对拆分的数量没有限制(会进行所有可能的拆分)。If sep is given, consecutive delimiters are not grouped together and are deemed to delimit empty strings (for example,如果给定sep,则连续分隔符不会分组在一起,并被视为分隔空字符串(例如,'1,,2'.split(',')
returns['1', '', '2']
).'1,,2'.split(',')
返回['1', '', '2']
)。The sep argument may consist of multiple characters (for example,sep参数可能由多个字符组成(例如,'1<>2<>3'.split('<>')
returns['1', '2', '3']
).'1<>2<>3'.split('<>')
返回['1', '2', '3']
)。Splitting an empty string with a specified separator returns使用指定的分隔符拆分空字符串将返回['']
.['']
。For example:例如:>>> '1,2,3'.split(',')
['1', '2', '3']
>>> '1,2,3'.split(',', maxsplit=1)
['1', '2,3']
>>> '1,2,,3,'.split(',')
['1', '2', '', '3', '']If sep is not specified or is如果未指定sep或sep为None
, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace.None
,则应用不同的拆分算法:连续空格的运行被视为单个分隔符,如果字符串具有前导或尾随空格,则结果的开头或结尾将不包含空字符串。Consequently, splitting an empty string or a string consisting of just whitespace with a因此,拆分一个空字符串或一个只包含空格且带有None
separator returns[]
.None
分隔符的字符串将返回[]
。For example:例如:>>> '1 2 3'.split()
['1', '2', '3']
>>> '1 2 3'.split(maxsplit=1)
['1', '2 3']
>>> ' 1 2 3 '.split()
['1', '2', '3']
-
str.
splitlines
(keepends=False)¶ Return a list of the lines in the string, breaking at line boundaries.返回字符串中的行列表,在行边界处打断。Line breaks are not included in the resulting list unless keepends is given and true.除非给定了keepends且为true
,否则换行符不包括在结果列表中。This method splits on the following line boundaries.此方法在以下线边界上拆分。In particular, the boundaries are a superset of universal newlines.特别是,边界是通用换行符的超集。Representation代表性Description描述\n
Line Feed线路馈线\r
Carriage Return回车符\r\n
Carriage Return + Line Feed回车+换行\v
or\x0b
Line Tabulation行列表\f
or\x0c
Form Feed表单馈送\x1c
File Separator文件分隔符\x1d
Group Separator组分隔符\x1e
Record Separator记录分隔符\x85
Next Line (C1 Control Code)下一行(C1控制代码)\u2028
Line Separator行分隔符\u2029
Paragraph Separator段落分隔符Changed in version 3.2:版本3.2中更改:\v
and\f
added to list of line boundaries.\v
和\f
已添加到线边界列表中。For example:例如:>>> 'ab c\n\nde fg\rkl\r\n'.splitlines()
['ab c', '', 'de fg', 'kl']
>>> 'ab c\n\nde fg\rkl\r\n'.splitlines(keepends=True)
['ab c\n', '\n', 'de fg\r', 'kl\r\n']Unlike与给定分隔符字符串sep时的split()
when a delimiter string sep is given, this method returns an empty list for the empty string, and a terminal line break does not result in an extra line:split()
不同,此方法为空字符串返回一个空列表,并且换行符不会产生额外的行:>>> "".splitlines()
[]
>>> "One line\n".splitlines()
['One line']For comparison,为了进行比较,split('\n')
gives:split('\n')
给出:>>> ''.split('\n')
['']
>>> 'Two lines\n'.split('\n')
['Two lines', '']
-
str.
startswith
(prefix[, start[, end]])¶ Return如果字符串以prefix开头,则返回True
if string starts with the prefix, otherwise returnFalse
.True
,否则返回False
。prefix can also be a tuple of prefixes to look for.prefix也可以是要查找的前缀元组。With optional start, test string beginning at that position.使用可选的start,测试从该位置开始的字符串。With optional end, stop comparing string at that position.使用可选end,停止在该位置比较字符串。
-
str.
strip
([chars])¶ Return a copy of the string with the leading and trailing characters removed.返回删除前导字符和尾随字符的字符串副本。The chars argument is a string specifying the set of characters to be removed.chars参数是一个字符串,指定要删除的字符集。If omitted or如果省略或为None
, the chars argument defaults to removing whitespace.None
,chars参数默认为删除空白。The chars argument is not a prefix or suffix; rather, all combinations of its values are stripped:chars参数不是前缀或后缀;相反,其值的所有组合都被剥离:>>> ' spacious '.strip()
'spacious'
>>> 'www.example.com'.strip('cmowz.')
'example'The outermost leading and trailing chars argument values are stripped from the string.最外层的前导字符和尾随字符参数值将从字符串中剥离。Characters are removed from the leading end until reaching a string character that is not contained in the set of characters in chars.字符从前端删除,直到到达chars中的字符集中不包含的字符串为止。A similar action takes place on the trailing end.尾端也会发生类似的动作。For example:例如:>>> comment_string = '#....... Section 3.2.1 Issue #32 .......'
>>> comment_string.strip('.#! ')
'Section 3.2.1 Issue #32'
-
str.
swapcase
()¶ Return a copy of the string with uppercase characters converted to lowercase and vice versa.返回将大写字符转换为小写的字符串副本,反之亦然。Note that it is not necessarily true that请注意,s.swapcase().swapcase() == s
.s.swapcase().swapcase() == s
不一定是True
。
-
str.
title
()¶ Return a titlecased version of the string where words start with an uppercase character and the remaining characters are lowercase.返回字符串的基于标题的版本,其中单词以大写字符开头,其余字符为小写。For example:例如:>>> 'Hello world'.title()
'Hello World'The algorithm uses a simple language-independent definition of a word as groups of consecutive letters.该算法使用一个简单的独立于语言的定义,将一个单词定义为一组连续的字母。The definition works in many contexts but it means that apostrophes in contractions and possessives form word boundaries, which may not be the desired result:这个定义在许多上下文中都适用,但它意味着缩略语和所有格中的撇号形成了单词边界,这可能不是期望的结果:>>> "they're bill's friends from the UK".title()
"They'Re Bill'S Friends From The Uk"Thestring.capwords()
function does not have this problem, as it splits words on spaces only.string.capwords()
函数没有这个问题,因为它只在空格上拆分单词。Alternatively, a workaround for apostrophes can be constructed using regular expressions:或者,可以使用正则表达式构造撇号的变通方法:>>> import re
>>> def titlecase(s):
... return re.sub(r"[A-Za-z]+('[A-Za-z]+)?",
... lambda mo: mo.group(0).capitalize(),
... s)
...
>>> titlecase("they're bill's friends.")
"They're Bill's Friends."
-
str.
translate
(table)¶ Return a copy of the string in which each character has been mapped through the given translation table.返回字符串的副本,其中每个字符已通过给定的转换表映射。The table must be an object that implements indexing via该表必须是通过__getitem__()
, typically a mapping or sequence.__getitem__()
实现索引的对象,通常是mapping或sequence。When indexed by a Unicode ordinal (an integer), the table object can do any of the following: return a Unicode ordinal or a string, to map the character to one or more other characters; return当按Unicode序号(整数)编制索引时,表对象可以执行以下任一操作:返回Unicode序号或字符串,将字符映射到一个或多个其他字符;返回None
, to delete the character from the return string; or raise aLookupError
exception, to map the character to itself.None
,从返回字符串中删除该字符;或引发LookupError
异常,以将角色映射到自身。You can use可以使用str.maketrans()
to create a translation map from character-to-character mappings in different formats.str.maketrans()
创建不同格式的字符到字符映射的转换映射。See also the有关自定义字符映射的更灵活方法,请参阅codecs
module for a more flexible approach to custom character mappings.codecs
(编解码器)模块。
-
str.
upper
()¶ Return a copy of the string with all the cased characters 4 converted to uppercase.返回字符串的副本,其中所有大小写字符4都转换为大写。Note that请注意,如果s.upper().isupper()
might beFalse
ifs
contains uncased characters or if the Unicode category of the resulting character(s) is not “Lu” (Letter, uppercase), but e.g. “Lt” (Letter, titlecase).s
包含未加密的字符,或者如果结果字符的Unicode类别不是“Lu”(字母,大写),而是“Lt”(字母,titlecase),s.upper().isupper()
可能为False
。The uppercasing algorithm used is described in section 3.13 of the Unicode Standard.Unicode标准第3.13节描述了使用的大写算法。
-
str.
zfill
(width)¶ Return a copy of the string left filled with ASCII返回由ASCII'0'
digits to make a string of length width.'0'
数字填充的字符串的副本,以生成长度width的字符串。A leading sign prefix (前导符号前缀('+'
/'-'
) is handled by inserting the padding after the sign character rather than before.'+'
/'-'
)是通过在符号字符之后而不是之前插入填充来处理的。The original string is returned if width is less than or equal to如果len(s)
.小于或等于
len(s)
,则返回原始字符串。For example:例如:>>> "42".zfill(5)
'00042'
>>> "-42".zfill(5)
'-0042'
printf
-style String Formatting:样式字符串格式¶
Note
The formatting operations described here exhibit a variety of quirks that lead to a number of common errors (such as failing to display tuples and dictionaries correctly). 这里描述的格式化操作表现出各种各样的怪癖,这些怪癖会导致许多常见错误(例如无法正确显示元组和字典)。Using the newer formatted string literals, the 使用较新的格式化字符串文本、str.format()
interface, or template strings may help avoid these errors. str.format()
接口或模板字符串可能有助于避免这些错误。Each of these alternatives provides their own trade-offs and benefits of simplicity, flexibility, and/or extensibility.这些备选方案中的每一个都提供了各自的折衷方案以及简单性、灵活性和/或可扩展性的好处。
String objects have one unique built-in operation: the 字符串对象有一个唯一的内置操作:%
operator (modulo). %
运算符(模)。This is also known as the string formatting or interpolation operator. 这也称为字符串格式或插值运算符。Given 给定format % values
(where format is a string), %
conversion specifications in format are replaced with zero or more elements of values. format % values
(其中format是字符串),%
格式中的转换规范将替换为零个或多个values元素。The effect is similar to using the 其效果类似于在C语言中使用sprintf()
in the C language.sprintf()
。
If format requires a single argument, values may be a single non-tuple object. 如果format需要单个参数,则values可以是单个非元组对象。5 Otherwise, values must be a tuple with exactly the number of items specified by the format string, or a single mapping object (for example, a dictionary).否则,values必须是具有格式字符串指定的项数的元组,或者是单个映射对象(例如,字典)。
A conversion specifier contains two or more characters and has the following components, which must occur in this order:转换说明符包含两个或多个字符,并具有以下组件,这些组件必须按以下顺序出现:
The'%'
character, which marks the start of the specifier.'%'
字符,它标记说明符的开头。Mapping key (optional), consisting of a parenthesised sequence of characters (for example,映射键(可选),由带括号的字符序列组成(例如,(somename)
).(somename)
)。Conversion flags (optional), which affect the result of some conversion types.转换标志(可选),影响某些转换类型的结果。Minimum field width (optional).最小字段宽度(可选)。If specified as an如果指定为'*'
(asterisk), the actual width is read from the next element of the tuple in values, and the object to convert comes after the minimum field width and optional precision.'*'
(星号),则从values中元组的下一个元素读取实际宽度,要转换的对象位于最小字段宽度和可选精度之后。Precision (optional), given as a精度(可选),以'.'
(dot) followed by the precision.'.'
表示(点)后跟精度。If specified as如果指定为'*'
(an asterisk), the actual precision is read from the next element of the tuple in values, and the value to convert comes after the precision.'*'
(星号),则从values中元组的下一个元素读取实际精度,要转换的值位于精度之后。Length modifier (optional).长度修改器(可选)。Conversion type.转换类型。
When the right argument is a dictionary (or other mapping type), then the formats in the string must include a parenthesised mapping key into that dictionary inserted immediately after the 如果正确的参数是字典(或其他映射类型),则字符串中的格式必须包含一个带括号的映射键,该键插入紧接'%'
character. '%'
字符之后的字典中。The mapping key selects the value to be formatted from the mapping. 映射键从映射中选择要格式化的值。For example:例如:
>>> print('%(language)s has %(number)03d quote types.' %
... {'language': "Python", "number": 2})
Python has 002 quote types.
In this case no 在这种情况下,格式中可能不会出现*
specifiers may occur in a format (since they require a sequential parameter list).*
说明符(因为它们需要顺序参数列表)。
The conversion flag characters are:转换标志字符为:
|
|
---|---|
|
|
|
|
|
|
|
|
|
|
A length modifier (长度修饰符(h
, l
, or L
) may be present, but is ignored as it is not necessary for Python – so e.g. %ld
is identical to %d
.h
、l
或L
)可能存在,但会被忽略,因为Python不需要它-因此,例如%ld
与%d
相同。
The conversion types are:转换类型包括:
|
|
|
---|---|---|
|
|
|
|
|
|
|
|
(1) |
|
|
(6) |
|
|
(2) |
|
|
(2) |
|
|
(3) |
|
|
(3) |
|
|
(3) |
|
|
(3) |
|
|
(4) |
|
|
(4) |
|
|
|
|
|
(5) |
|
|
(5) |
|
|
(5) |
|
|
Notes:
The alternate form causes a leading octal specifier (替代形式导致在第一个数字之前插入前导八进制说明符('0o'
) to be inserted before the first digit.'0o'
)。The alternate form causes a leading备用格式导致在第一个数字之前插入前导'0x'
or'0X'
(depending on whether the'x'
or'X'
format was used) to be inserted before the first digit.'0x'
或'0X'
(取决于使用的是'x'
或'X'
格式)。The alternate form causes the result to always contain a decimal point, even if no digits follow it.替代形式使结果始终包含小数点,即使其后没有数字。The precision determines the number of digits after the decimal point and defaults to 6.精度确定小数点后的位数,默认为6。The alternate form causes the result to always contain a decimal point, and trailing zeroes are not removed as they would otherwise be.替代形式会导致结果始终包含小数点,并且不会像其他形式那样删除尾随的零。The precision determines the number of significant digits before and after the decimal point and defaults to 6.精度确定小数点前后的有效位数,默认为6。If precision is如果精度为N
, the output is truncated toN
characters.N
,则输出将截断为N
个字符。
Since Python strings have an explicit length, 由于Python字符串具有显式长度,%s
conversions do not assume that '\0'
is the end of the string.%s
转换不会假定'\0'
是字符串的结尾。
Changed in version 3.1: 版本3.1中更改:绝对值超过1e50的数字的%f
conversions for numbers whose absolute value is over 1e50 are no longer replaced by %g
conversions.%f
转换不再替换为%g
转换。
Binary Sequence Types — 二进制序列类型:bytes
, 、bytearray
, 、memoryview
¶
The core built-in types for manipulating binary data are 用于操作二进制数据的核心内置类型是bytes
and bytearray
. bytes
和bytearray
。They are supported by 它们由memoryview
which uses the buffer protocol to access the memory of other binary objects without needing to make a copy.memoryview
支持,memoryview
使用缓冲协议访问其他二进制对象的内存,而无需创建副本。
The array
module supports efficient storage of basic data types like 32-bit integers and IEEE754 double-precision floating values.array
模块支持高效存储基本数据类型,如32位整数和IEEE754双精度浮点值。
Bytes Objects字节对象¶
Bytes objects are immutable sequences of single bytes. 字节对象是单个字节的不可变序列。Since many major binary protocols are based on the ASCII text encoding, bytes objects offer several methods that are only valid when working with ASCII compatible data and are closely related to string objects in a variety of other ways.由于许多主要的二进制协议都基于ASCII文本编码,字节对象提供了几种方法,这些方法仅在处理ASCII兼容数据时有效,并以各种其他方式与字符串对象密切相关。
-
class
bytes
([source[, encoding[, errors]]])¶ Firstly, the syntax for bytes literals is largely the same as that for string literals, except that a首先,字节文字的语法与字符串文字的语法基本相同,只是添加了b
prefix is added:b
前缀:Single quotes:单引号:b'still allows embedded "double" quotes'
Double quotes:双引号:b"still allows embedded 'single' quotes"
Triple quoted:三引号:b'''3 single quotes'''
,b"""3 double quotes"""
Only ASCII characters are permitted in bytes literals (regardless of the declared source code encoding).字节文字中只允许使用ASCII字符(无论声明的源代码编码如何)。Any binary values over 127 must be entered into bytes literals using the appropriate escape sequence.任何超过127的二进制值都必须使用适当的转义序列输入字节文本。As with string literals, bytes literals may also use a与字符串文字一样,字节文字也可以使用r
prefix to disable processing of escape sequences.r
前缀来禁用转义序列的处理。See String and Bytes literals for more about the various forms of bytes literal, including supported escape sequences.有关字节文字的各种形式(包括支持的转义序列)的更多信息,请参阅字符串和字节文字。While bytes literals and representations are based on ASCII text, bytes objects actually behave like immutable sequences of integers, with each value in the sequence restricted such that虽然字节文字和表示基于ASCII文本,但字节对象的行为实际上类似于不可变的整数序列,序列中的每个值都受到限制,因此0 <= x < 256
(attempts to violate this restriction will triggerValueError
).0 <= x < 256
(尝试违反此限制将触发ValueError
)。This is done deliberately to emphasise that while many binary formats include ASCII based elements and can be usefully manipulated with some text-oriented algorithms, this is not generally the case for arbitrary binary data (blindly applying text processing algorithms to binary data formats that are not ASCII compatible will usually lead to data corruption).这样做是为了强调,虽然许多二进制格式包括基于ASCII的元素,并且可以使用一些面向文本的算法进行有效操作,但对于任意二进制数据,通常情况下并非如此(盲目地将文本处理算法应用于与ASCII不兼容的二进制数据格式通常会导致数据损坏)。In addition to the literal forms, bytes objects can be created in a number of other ways:除了文字形式外,还可以通过多种其他方式创建字节对象:A zero-filled bytes object of a specified length:指定长度的零填充字节对象:bytes(10)
From an iterable of integers:从整数的iterable:bytes(range(20))
Copying existing binary data via the buffer protocol:通过缓冲协议复制现有二进制数据:bytes(obj)
Also see the bytes built-in.另请参见内置的字节。Since 2 hexadecimal digits correspond precisely to a single byte, hexadecimal numbers are a commonly used format for describing binary data.由于两个十六进制数字精确地对应于一个字节,十六进制数字是描述二进制数据的常用格式。Accordingly, the bytes type has an additional class method to read data in that format:因此,字节类型有一个额外的类方法来读取该格式的数据:-
classmethod
fromhex
(string)¶ This这个bytes
class method returns a bytes object, decoding the given string object.bytes
类方法返回一个bytes对象,对给定的string对象进行解码。The string must contain two hexadecimal digits per byte, with ASCII whitespace being ignored.字符串每个字节必须包含两个十六进制数字,ASCII空格将被忽略。>>> bytes.fromhex('2Ef0 F1f2 ')
b'.\xf0\xf1\xf2'Changed in version 3.7:版本3.7中更改:bytes.fromhex()
now skips all ASCII whitespace in the string, not just spaces.现在跳过字符串中的所有ASCII空格,而不仅仅是空格。
A reverse conversion function exists to transform a bytes object into its hexadecimal representation.存在一个反向转换函数,用于将字节对象转换为其十六进制表示形式。-
hex
([sep[, bytes_per_sep]])¶ Return a string object containing two hexadecimal digits for each byte in the instance.返回一个字符串对象,该对象包含实例中每个字节的两个十六进制数字。>>> b'\xf0\xf1\xf2'.hex()
'f0f1f2'If you want to make the hex string easier to read, you can specify a single character separator sep parameter to include in the output.如果要使十六进制字符串更易于读取,可以指定要包含在输出中的单字符分隔符sep参数。By default between each byte.默认情况下,每个字节之间。A second optional bytes_per_sep parameter controls the spacing.第二个可选的bytes_per_sep参数控制间距。Positive values calculate the separator position from the right, negative values from the left.正值从右侧计算分隔符位置,负值从左侧计算。>>> value = b'\xf0\xf1\xf2'
>>> value.hex('-')
'f0-f1-f2'
>>> value.hex('_', 2)
'f0_f1f2'
>>> b'UUDDLRLRAB'.hex(' ', -4)
'55554444 4c524c52 4142'New in version 3.5.版本3.5中新增。Changed in version 3.8:版本3.8中更改:bytes.hex()
now supports optional sep and bytes_per_sep parameters to insert separators between bytes in the hex output.现在支持可选的sep和bytes_per_sep参数,以便在十六进制输出中的字节之间插入分隔符。
Since bytes objects are sequences of integers (akin to a tuple), for a bytes object b, 由于bytes对象是整数序列(类似于元组),对于bytes对象b,b[0]
will be an integer, while b[0:1]
will be a bytes object of length 1. b[0]
将是整数,而b[0:1]
将是长度为1的bytes对象。(This contrasts with text strings, where both indexing and slicing will produce a string of length 1)(这与文本字符串形成对比,在文本字符串中,索引和切片都将生成长度为1的字符串)
The representation of bytes objects uses the literal format (字节对象的表示使用文字格式(b'...'
) since it is often more useful than e.g. bytes([46, 46, 46])
. b'...'
),因为它通常比bytes([46, 46, 46])
更有用。You can always convert a bytes object into a list of integers using 您始终可以使用list(b)
.list(b)
将bytes对象转换为整数列表。
Note
For Python 2.x users: In the Python 2.x series, a variety of implicit conversions between 8-bit strings (the closest thing 2.x offers to a built-in binary data type) and Unicode strings were permitted. 对于Python 2x用户:在Python 2x系列中,允许在8位字符串(最接近内置二进制数据类型的2x)和Unicode字符串之间进行各种隐式转换。This was a backwards compatibility workaround to account for the fact that Python originally only supported 8-bit text, and Unicode text was a later addition. 这是一种向后兼容性解决方案,可以解释Python最初只支持8位文本,而Unicode文本是后来添加的。In Python 3.x, those implicit conversions are gone - conversions between 8-bit binary data and Unicode text must be explicit, and bytes and string objects will always compare unequal.在Python 3x中,这些隐式转换已经消失了——8位二进制数据和Unicode文本之间的转换必须是显式的,字节和字符串对象之间的比较总是不相等的。
Bytearray ObjectsBytearray对象¶
bytearray
objects are a mutable counterpart to 对象是bytes
objects.bytes
对象的可变对应项。
-
class
bytearray
([source[, encoding[, errors]]])¶ There is no dedicated literal syntax for bytearray objects, instead they are always created by calling the constructor:bytearray对象没有专用的文字语法,而是始终通过调用构造函数来创建:Creating an empty instance:创建空实例:bytearray()
Creating a zero-filled instance with a given length:创建具有给定长度的零填充实例:bytearray(10)
From an iterable of integers:从整数的iterable:bytearray(range(20))
Copying existing binary data via the buffer protocol:通过缓冲协议复制现有二进制数据:bytearray(b'Hi!')
As bytearray objects are mutable, they support the mutable sequence operations in addition to the common bytes and bytearray operations described in Bytes and Bytearray Operations.由于bytearray对象是可变的,因此除了Bytes和Bytearray操作中描述的公共bytes和bytearray操作之外,它们还支持可变序列操作。Also see the bytearray built-in.另请参见bytearray内置。Since 2 hexadecimal digits correspond precisely to a single byte, hexadecimal numbers are a commonly used format for describing binary data. Accordingly, the bytearray type has an additional class method to read data in that format:由于两个十六进制数字精确地对应于一个字节,十六进制数字是描述二进制数据的常用格式。因此,bytearray类型有一个额外的类方法来读取该格式的数据:-
classmethod
fromhex
(string)¶ This这个bytearray
class method returns bytearray object, decoding the given string object.bytearray
类方法返回bytearray
对象,对给定的string对象进行解码。The string must contain two hexadecimal digits per byte, with ASCII whitespace being ignored.字符串每个字节必须包含两个十六进制数字,ASCII空格将被忽略。>>> bytearray.fromhex('2Ef0 F1f2 ')
bytearray(b'.\xf0\xf1\xf2')Changed in version 3.7:在版本3.7中更改:bytearray.fromhex()
now skips all ASCII whitespace in the string, not just spaces.现在跳过字符串中的所有ASCII空格,而不仅仅是空格。
A reverse conversion function exists to transform a bytearray object into its hexadecimal representation.存在一个反向转换函数,用于将bytearray对象转换为其十六进制表示形式。-
hex
([sep[, bytes_per_sep]])¶ Return a string object containing two hexadecimal digits for each byte in the instance.返回一个字符串对象,该对象包含实例中每个字节的两个十六进制数字。>>> bytearray(b'\xf0\xf1\xf2').hex()
'f0f1f2'New in version 3.5.版本3.5中新增。Changed in version 3.8:在版本3.8中更改:Similar to与bytes.hex()
,bytearray.hex()
now supports optional sep and bytes_per_sep parameters to insert separators between bytes in the hex output.bytes.hex()
类似,bytearray.hex()
现在支持可选的sep和bytes_per_sep参数,以便在十六进制输出中的字节之间插入分隔符。
Since bytearray objects are sequences of integers (akin to a list), for a bytearray object b, 由于bytearray对象是整数序列(类似于列表),因此对于bytearray对象b,b[0]
will be an integer, while b[0:1]
will be a bytearray object of length 1. b[0]
将是整数,而b[0:1]
将是长度为1的bytearray对象。(This contrasts with text strings, where both indexing and slicing will produce a string of length 1)(这与文本字符串形成对比,在文本字符串中,索引和切片都将生成长度为1的字符串)
The representation of bytearray objects uses the bytes literal format (bytearray对象的表示使用字节文字格式(bytearray(b'...')
) since it is often more useful than e.g. bytearray([46, 46, 46])
. bytearray(b'...')
),因为它通常比bytearray([46, 46, 46])
更有用。You can always convert a bytearray object into a list of integers using 始终可以使用list(b)
.list(b)
将bytearray对象转换为整数列表。
Bytes and Bytearray Operations字节和字节数组操作¶
Both bytes and bytearray objects support the common sequence operations. bytes和bytearray对象都支持公共序列操作。They interoperate not just with operands of the same type, but with any bytes-like object. 它们不仅与相同类型的操作数互操作,还与任何类似字节的对象互操作。Due to this flexibility, they can be freely mixed in operations without causing errors. 由于这种灵活性,它们可以在操作中自由混合,而不会导致错误。However, the return type of the result may depend on the order of operands.但是,结果的返回类型可能取决于操作数的顺序。
Note
The methods on bytes and bytearray objects don’t accept strings as their arguments, just as the methods on strings don’t accept bytes as their arguments. bytes和bytearray对象上的方法不接受字符串作为其参数,就像字符串上的方法不接受bytes作为其参数一样。For example, you have to write:例如,您必须编写:
a = "abc"
b = a.replace("a", "f")
and:并且:
a = b"abc"
b = a.replace(b"a", b"f")
Some bytes and bytearray operations assume the use of ASCII compatible binary formats, and hence should be avoided when working with arbitrary binary data. 某些字节和字节数组操作假定使用ASCII兼容的二进制格式,因此在处理任意二进制数据时应避免使用。These restrictions are covered below.这些限制如下所述。
Note
Using these ASCII based operations to manipulate binary data that is not stored in an ASCII based format may lead to data corruption.使用这些基于ASCII的操作来操作未以基于ASCII的格式存储的二进制数据可能会导致数据损坏。
The following methods on bytes and bytearray objects can be used with arbitrary binary data.字节和字节数组对象的以下方法可用于任意二进制数据。
-
bytes.
count
(sub[, start[, end]])¶ -
bytearray.
count
(sub[, start[, end]])¶ Return the number of non-overlapping occurrences of subsequence sub in the range [start, end].返回[start, end]范围内子序列sub的非重叠出现次数。Optional arguments start and end are interpreted as in slice notation.可选参数start和end被解释为切片表示法。The subsequence to search for may be any bytes-like object or an integer in the range 0 to 255.要搜索的子序列可以是任何类似字节的对象,也可以是0到255范围内的整数。Changed in version 3.3:在版本3.3中更改:Also accept an integer in the range 0 to 255 as the subsequence.还接受0到255范围内的整数作为子序列。
-
bytes.
removeprefix
(prefix, /)¶ -
bytearray.
removeprefix
(prefix, /)¶ If the binary data starts with the prefix string, return如果二进制数据以prefix字符串开头,则返回bytes[len(prefix):]
.bytes[len(prefix):]
。Otherwise, return a copy of the original binary data:否则,返回原始二进制数据的副本:>>> b'TestHook'.removeprefix(b'Test')
b'Hook'
>>> b'BaseTestCase'.removeprefix(b'Test')
b'BaseTestCase'The prefix may be any bytes-like object.prefix可以是任何类似字节的对象。Note
The bytearray version of this method does not operate in place - it always produces a new object, even if no changes were made.此方法的bytearray版本无法原地操作:它始终生成一个新对象,即使没有进行任何更改。New in version 3.9.版本3.9中新增。
-
bytes.
removesuffix
(suffix, /)¶ -
bytearray.
removesuffix
(suffix, /)¶ If the binary data ends with the suffix string and that suffix is not empty, return如果二进制数据以suffix字符串结尾,且该suffix不是空的,则返回bytes[:-len(suffix)]
.bytes[:-len(suffix)]
。Otherwise, return a copy of the original binary data:否则,返回原始二进制数据的副本:>>> b'MiscTests'.removesuffix(b'Tests')
b'Misc'
>>> b'TmpDirMixin'.removesuffix(b'Tests')
b'TmpDirMixin'The suffix may be any bytes-like object.suffix可以是任何类似字节的对象。Note
The bytearray version of this method does not operate in place - it always produces a new object, even if no changes were made.此方法的bytearray版本无法原地操作:它始终生成一个新对象,即使没有进行任何更改。New in version 3.9.版本3.9中新增。
-
bytes.
decode
(encoding='utf-8', errors='strict')¶ -
bytearray.
decode
(encoding='utf-8', errors='strict')¶ Return a string decoded from the given bytes.返回从给定字节解码的字符串。Default encoding is默认编码为'utf-8'
.'utf-8'
。errors may be given to set a different error handling scheme.可能会给出错误以设置不同的错误处理方案。The default for errors iserrors的默认值为'strict'
, meaning that encoding errors raise aUnicodeError
.'strict'
,这意味着编码错误会引发UnicodeError
。Other possible values are其他可能的值包括'ignore'
,'replace'
and any other name registered viacodecs.register_error()
, see section Error Handlers.'ignore'
、'replace'
和通过codecs.register_error()
注册的任何其他名称,请参阅错误处理程序一节。For a list of possible encodings, see section Standard Encodings.有关可能的编码列表,请参阅标准编码一节。By default, the errors argument is not checked for best performances, but only used at the first decoding error.默认情况下,不会检查errors参数以获得最佳性能,而是仅在第一次解码错误时使用。Enable the Python Development Mode, or use a debug build to check errors.启用Python开发模式,或使用调试构建来检查errors。Note
Passing the encoding argument to将encoding参数传递给str
allows decoding any bytes-like object directly, without needing to make a temporary bytes or bytearray object.str
允许直接解码任何类似字节的对象,而无需创建临时字节或bytearray对象。Changed in version 3.1:版本3.1中更改:Added support for keyword arguments.添加了对关键字参数的支持。Changed in version 3.9:版本3.9中更改:The errors is now checked in development mode and in debug mode.现在在开发模式和调试模式中检查errors。
-
bytes.
endswith
(suffix[, start[, end]])¶ -
bytearray.
endswith
(suffix[, start[, end]])¶ Return如果二进制数据以指定的suffix结尾,则返回True
if the binary data ends with the specified suffix, otherwise returnFalse
.True
,否则返回False
。suffix can also be a tuple of suffixes to look for.suffix也可以是要查找的后缀元组。With optional start, test beginning at that position.使用可选start,从该位置开始测试。With optional end, stop comparing at that position.使用可选end,在该位置停止进行比较。The suffix(es) to search for may be any bytes-like object.要搜索的后缀可以是任何类似字节的对象。
-
bytes.
find
(sub[, start[, end]])¶ -
bytearray.
find
(sub[, start[, end]])¶ Return the lowest index in the data where the subsequence sub is found, such that sub is contained in the slice返回找到子序列sub的数据中的最低索引,以便sub包含在切片s[start:end]
.s[start:end]
中。Optional arguments start and end are interpreted as in slice notation.可选参数start和end被解释为切片表示法。Return如果未找到sub,则返回-1
if sub is not found.-1
。The subsequence to search for may be any bytes-like object or an integer in the range 0 to 255.要搜索的子序列可以是任何类似字节的对象,也可以是0到255范围内的整数。Note
The只有在需要知道sub的位置时,才应使用find()
method should be used only if you need to know the position of sub.find()
方法。To check if sub is a substring or not, use the要检查sub是否为子字符串,请使用in
operator:in
运算符:>>> b'Py' in b'Python'
TrueChanged in version 3.3:版本3.3中更改:Also accept an integer in the range 0 to 255 as the subsequence.还接受0到255范围内的整数作为子序列。
-
bytes.
index
(sub[, start[, end]])¶ -
bytearray.
index
(sub[, start[, end]])¶ Like与find()
, but raiseValueError
when the subsequence is not found.find()
类似,但在未找到子序列时引发ValueError
。The subsequence to search for may be any bytes-like object or an integer in the range 0 to 255.要搜索的子序列可以是任何类似字节的对象,也可以是0到255范围内的整数。Changed in version 3.3:版本3.3中更改:Also accept an integer in the range 0 to 255 as the subsequence.还接受0到255范围内的整数作为子序列。
-
bytes.
join
(iterable)¶ -
bytearray.
join
(iterable)¶ Return a bytes or bytearray object which is the concatenation of the binary data sequences in iterable.返回字节或bytearray对象,该对象是iterable中二进制数据序列的串联。A如果iterable中有任何值不是类似字节的对象,包括TypeError
will be raised if there are any values in iterable that are not bytes-like objects, includingstr
objects.str
对象,则会引发TypeError
。The separator between elements is the contents of the bytes or bytearray object providing this method.元素之间的分隔符是提供此方法的bytes或bytearray对象的内容。
-
static
bytes.
maketrans
(from, to)¶ -
static
bytearray.
maketrans
(from, to)¶ This static method returns a translation table usable for此静态方法返回一个可用于bytes.translate()
that will map each character in from into the character at the same position in to; from and to must both be bytes-like objects and have the same length.bytes.translate()
的转换表,该表将from中的每个字符映射到to中相同位置的字符;from和to必须都是类似字节的对象,并且具有相同的长度。New in version 3.1.版本3.1中新增。
-
bytes.
partition
(sep)¶ -
bytearray.
partition
(sep)¶ Split the sequence at the first occurrence of sep, and return a 3-tuple containing the part before the separator, the separator itself or its bytearray copy, and the part after the separator.在sep第一次出现时拆分序列,并返回一个3元组,其中包含分隔符之前的部分、分隔符本身或其bytearray副本以及分隔符之后的部分。If the separator is not found, return a 3-tuple containing a copy of the original sequence, followed by two empty bytes or bytearray objects.如果未找到分隔符,则返回一个包含原始序列副本的3元组,后跟两个空字节或bytearray对象。The separator to search for may be any bytes-like object.要搜索的分隔符可以是任何类似字节的对象。
-
bytes.
replace
(old, new[, count])¶ -
bytearray.
replace
(old, new[, count])¶ Return a copy of the sequence with all occurrences of subsequence old replaced by new.返回序列的副本,其中所有出现的子序列old替换为new。If the optional argument count is given, only the first count occurrences are replaced.如果给定了可选的参数count,则只替换第一次出现的count。The subsequence to search for and its replacement may be any bytes-like object.要搜索和替换的子序列可以是任何类似字节的对象。Note
The bytearray version of this method does not operate in place - it always produces a new object, even if no changes were made.此方法的bytearray版本无法正常运行-它始终生成一个新对象,即使没有进行任何更改。
-
bytes.
rfind
(sub[, start[, end]])¶ -
bytearray.
rfind
(sub[, start[, end]])¶ Return the highest index in the sequence where the subsequence sub is found, such that sub is contained within返回找到子序列sub的序列中的最高索引,以便sub包含在s[start:end]
.s[start:end]
中。Optional arguments start and end are interpreted as in slice notation.可选参数start和end被解释为切片表示法。Return故障时返回-1
on failure.-1
。The subsequence to search for may be any bytes-like object or an integer in the range 0 to 255.要搜索的子序列可以是任何类似字节的对象,也可以是0到255范围内的整数。Changed in version 3.3:在版本3.3中更改:Also accept an integer in the range 0 to 255 as the subsequence.还接受0到255范围内的整数作为子序列。
-
bytes.
rindex
(sub[, start[, end]])¶ -
bytearray.
rindex
(sub[, start[, end]])¶ Like与rfind()
but raisesValueError
when the subsequence sub is not found.rfind()
类似,但在未找到子序列sub时引发ValueError
。The subsequence to search for may be any bytes-like object or an integer in the range 0 to 255.要搜索的子序列可以是任何类似字节的对象,也可以是0到255范围内的整数。Changed in version 3.3:在版本3.3中更改:Also accept an integer in the range 0 to 255 as the subsequence.还接受0到255范围内的整数作为子序列。
-
bytes.
rpartition
(sep)¶ -
bytearray.
rpartition
(sep)¶ Split the sequence at the last occurrence of sep, and return a 3-tuple containing the part before the separator, the separator itself or its bytearray copy, and the part after the separator.在sep的最后一次出现时拆分序列,并返回一个3元组,其中包含分隔符之前的部分、分隔符本身或其bytearray副本以及分隔符之后的部分。If the separator is not found, return a 3-tuple containing two empty bytes or bytearray objects, followed by a copy of the original sequence.如果未找到分隔符,则返回一个包含两个空字节或bytearray对象的3元组,后跟原始序列的副本。The separator to search for may be any bytes-like object.要搜索的分隔符可以是任何类似字节的对象。
-
bytes.
startswith
(prefix[, start[, end]])¶ -
bytearray.
startswith
(prefix[, start[, end]])¶ Return如果二进制数据以指定prefix开头,则返回True
if the binary data starts with the specified prefix, otherwise returnFalse
.True
,否则返回False
。prefix can also be a tuple of prefixes to look for.prefix也可以是要查找的前缀元组。With optional start, test beginning at that position.使用可选start,从该位置开始测试。With optional end, stop comparing at that position.使用可选end,停止在该位置进行比较。The prefix(es) to search for may be any bytes-like object.要搜索的前缀可以是任何类似字节的对象。
-
bytes.
translate
(table, /, delete=b'')¶ -
bytearray.
translate
(table, /, delete=b'')¶ Return a copy of the bytes or bytearray object where all bytes occurring in the optional argument delete are removed, and the remaining bytes have been mapped through the given translation table, which must be a bytes object of length 256.返回bytes或bytearray对象的副本,其中删除可选参数delete中出现的所有字节,并通过给定的转换表映射剩余字节,转换表必须是长度为256的bytes对象。You can use the可以使用bytes.maketrans()
method to create a translation table.bytes.maketrans()
方法创建转换表。Set the table argument to对于仅删除字符的翻译,请将table参数设置为None
for translations that only delete characters:None
:>>> b'read this short text'.translate(None, b'aeiou')
b'rd ths shrt txt'Changed in version 3.6:在版本3.6中更改:deleteis now supported as a keyword argument.现在支持作为关键字参数。
The following methods on bytes and bytearray objects have default behaviours that assume the use of ASCII compatible binary formats, but can still be used with arbitrary binary data by passing appropriate arguments. 以下关于bytes和bytearray对象的方法具有默认行为,假定使用ASCII兼容的二进制格式,但通过传递适当的参数,仍然可以用于任意二进制数据。Note that all of the bytearray methods in this section do not operate in place, and instead produce new objects.请注意,本节中的所有bytearray方法都不是原地操作,而是生成新对象。
-
bytes.
center
(width[, fillbyte])¶ -
bytearray.
center
(width[, fillbyte])¶ Return a copy of the object centered in a sequence of length width.返回以长度width序列为中心的对象副本。Padding is done using the specified fillbyte (default is an ASCII space).填充是使用指定的fillbyte完成的(默认为ASCII空格)。For对于bytes
objects, the original sequence is returned if width is less than or equal tolen(s)
.bytes
对象,如果width小于或等于len(s)
,则返回原始序列。Note
The bytearray version of this method does not operate in place - it always produces a new object, even if no changes were made.此方法的bytearray版本无法原地操作-它始终生成一个新对象,即使没有进行任何更改。
-
bytes.
ljust
(width[, fillbyte])¶ -
bytearray.
ljust
(width[, fillbyte])¶ Return a copy of the object left justified in a sequence of length width.返回按长度width顺序左对齐的对象副本。Padding is done using the specified fillbyte (default is an ASCII space).填充是使用指定的fillbyte完成的(默认为ASCII空格)。For对于bytes
objects, the original sequence is returned if width is less than or equal tolen(s)
.bytes
对象,如果width小于或等于len(s)
,则返回原始序列。Note
The bytearray version of this method does not operate in place - it always produces a new object, even if no changes were made.此方法的bytearray版本无法原地操作:它始终生成一个新对象,即使没有进行任何更改。
-
bytes.
lstrip
([chars])¶ -
bytearray.
lstrip
([chars])¶ Return a copy of the sequence with specified leading bytes removed.返回删除了指定前导字节的序列副本。The chars argument is a binary sequence specifying the set of byte values to be removed - the name refers to the fact this method is usually used with ASCII characters.chars参数是一个二进制序列,指定要删除的字节值集-名称表示此方法通常用于ASCII字符。If omitted or如果省略或为None
, the chars argument defaults to removing ASCII whitespace.None
,chars参数默认为删除ASCII空格。The chars argument is not a prefix; rather, all combinations of its values are stripped:chars参数不是前缀;相反,其值的所有组合都被剥离:>>> b' spacious '.lstrip()
b'spacious '
>>> b'www.example.com'.lstrip(b'cmowz.')
b'example.com'The binary sequence of byte values to remove may be any bytes-like object.要删除的字节值的二进制序列可以是任何类似于字节的对象。See有关将删除单个前缀字符串而不是所有字符集的方法,请参阅removeprefix()
for a method that will remove a single prefix string rather than all of a set of characters.removeprefix()
。For example:例如:>>> b'Arthur: three!'.lstrip(b'Arthur: ')
b'ee!'
>>> b'Arthur: three!'.removeprefix(b'Arthur: ')
b'three!'Note
The bytearray version of this method does not operate in place - it always produces a new object, even if no changes were made.此方法的bytearray版本无法原地操作:它始终生成一个新对象,即使没有进行任何更改。
-
bytes.
rjust
(width[, fillbyte])¶ -
bytearray.
rjust
(width[, fillbyte])¶ Return a copy of the object right justified in a sequence of length width.返回按长度width的序列右对齐的对象副本。Padding is done using the specified fillbyte (default is an ASCII space).填充是使用指定的fillbyte完成的(默认为ASCII空格)。For对于bytes
objects, the original sequence is returned if width is less than or equal tolen(s)
.bytes
对象,如果width小于或等于len(s)
,则返回原始序列。Note
The bytearray version of this method does not operate in place - it always produces a new object, even if no changes were made.此方法的bytearray版本无法原地操作:它始终生成一个新对象,即使没有进行任何更改。
-
bytes.
rsplit
(sep=None, maxsplit=- 1)¶ -
bytearray.
rsplit
(sep=None, maxsplit=- 1)¶ Split the binary sequence into subsequences of the same type, using sep as the delimiter string.使用sep作为分隔符字符串,将二进制序列拆分为相同类型的子序列。If maxsplit is given, at most maxsplit splits are done, the rightmost ones.如果给定maxsplit,则最多执行maxsplit拆分,最右边的拆分。If sep is not specified or如果未指定sep或指定为None
, any subsequence consisting solely of ASCII whitespace is a separator.None
,则仅由ASCII空格组成的任何子序列都是分隔符。Except for splitting from the right,除了从右侧拆分之外,rsplit()
behaves likesplit()
which is described in detail below.rsplit()
的行为类似于split()
,下面将对其进行详细描述。
-
bytes.
rstrip
([chars])¶ -
bytearray.
rstrip
([chars])¶ Return a copy of the sequence with specified trailing bytes removed.返回删除了指定尾随字节的序列副本。The chars argument is a binary sequence specifying the set of byte values to be removed - the name refers to the fact this method is usually used with ASCII characters.chars参数是一个二进制序列,指定要删除的字节值集-名称表示此方法通常用于ASCII字符。If omitted or如果省略或为None
, the chars argument defaults to removing ASCII whitespace.None
,chars参数默认为删除ASCII空格。The chars argument is not a suffix; rather, all combinations of its values are stripped:chars参数不是后缀;相反,其值的所有组合都被剥离:>>> b' spacious '.rstrip()
b' spacious'
>>> b'mississippi'.rstrip(b'ipz')
b'mississ'The binary sequence of byte values to remove may be any bytes-like object.要删除的字节值的二进制序列可以是任何类似字节的对象。See有关将删除单个后缀字符串而不是所有字符集的方法,请参阅removesuffix()
for a method that will remove a single suffix string rather than all of a set of characters.removesuffix()
。For example:例如:>>> b'Monty Python'.rstrip(b' Python')
b'M'
>>> b'Monty Python'.removesuffix(b' Python')
b'Monty'Note
The bytearray version of this method does not operate in place - it always produces a new object, even if no changes were made.此方法的bytearray版本无法原地操作:它始终生成一个新对象,即使没有进行任何更改。
-
bytes.
split
(sep=None, maxsplit=- 1)¶ -
bytearray.
split
(sep=None, maxsplit=- 1)¶ Split the binary sequence into subsequences of the same type, using sep as the delimiter string.使用sep作为分隔符字符串,将二进制序列拆分为相同类型的子序列。If maxsplit is given and non-negative, at most maxsplit splits are done (thus, the list will have at most如果给定maxsplit且为非负,则最多完成maxsplit拆分(因此,列表最多包含maxsplit+1
elements).maxsplit+1
个元素)。If maxsplit is not specified or is如果maxsplit未指定或为-1
, then there is no limit on the number of splits (all possible splits are made).-1
,则对拆分数量没有限制(所有可能的拆分都已进行)。If sep is given, consecutive delimiters are not grouped together and are deemed to delimit empty subsequences (for example,如果给定sep,则连续分隔符不会分组在一起,并被视为分隔空的子序列(例如,b'1,,2'.split(b',')
returns[b'1', b'', b'2']
).b'1,,2'.split(b',')
返回[b'1', b'', b'2']
)。The sep argument may consist of a multibyte sequence (for example,sep参数可能由多字节序列组成(例如,b'1<>2<>3'.split(b'<>')
returns[b'1', b'2', b'3']
).b'1<>2<>3'.split(b'<>')
返回[b'1', b'2', b'3']
)。Splitting an empty sequence with a specified separator returns使用指定的分隔符拆分空序列会根据要拆分的对象的类型返回[b'']
or[bytearray(b'')]
depending on the type of object being split.[b'']
或[bytearray(b'')]
。The sep argument may be any bytes-like object.sep参数可以是任何类似字节的对象。For example:例如:>>> b'1,2,3'.split(b',')
[b'1', b'2', b'3']
>>> b'1,2,3'.split(b',', maxsplit=1)
[b'1', b'2,3']
>>> b'1,2,,3,'.split(b',')
[b'1', b'2', b'', b'3', b'']If sep is not specified or is如果未指定sep或为None
, a different splitting algorithm is applied: runs of consecutive ASCII whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the sequence has leading or trailing whitespace.None
,则应用不同的拆分算法:连续ASCII空格的运行被视为单个分隔符,如果序列具有前导或尾随空格,则结果的开头或结尾将不包含空字符串。Consequently, splitting an empty sequence or a sequence consisting solely of ASCII whitespace without a specified separator returns因此,拆分空序列或仅由ASCII空格组成且没有指定分隔符的序列将返回[]
.[]
。For example:例如:>>> b'1 2 3'.split()
[b'1', b'2', b'3']
>>> b'1 2 3'.split(maxsplit=1)
[b'1', b'2 3']
>>> b' 1 2 3 '.split()
[b'1', b'2', b'3']
-
bytes.
strip
([chars])¶ -
bytearray.
strip
([chars])¶ Return a copy of the sequence with specified leading and trailing bytes removed.返回删除了指定前导字节和尾随字节的序列副本。The chars argument is a binary sequence specifying the set of byte values to be removed - the name refers to the fact this method is usually used with ASCII characters.chars参数是一个二进制序列,指定要删除的字节值集-名称表示此方法通常用于ASCII字符。If omitted or如果省略或为None
, the chars argument defaults to removing ASCII whitespace.None
,则chars参数默认为删除ASCII空格。The chars argument is not a prefix or suffix; rather, all combinations of its values are stripped:chars参数不是前缀或后缀;相反,其值的所有组合都被剥离:>>> b' spacious '.strip()
b'spacious'
>>> b'www.example.com'.strip(b'cmowz.')
b'example'The binary sequence of byte values to remove may be any bytes-like object.要删除的字节值的二进制序列可以是任何类似于字节的对象。Note
The bytearray version of this method does not operate in place - it always produces a new object, even if no changes were made.此方法的bytearray版本无法原地操作:它始终生成一个新对象,即使没有进行任何更改。
The following methods on bytes and bytearray objects assume the use of ASCII compatible binary formats and should not be applied to arbitrary binary data. 以下关于bytes和bytearray对象的方法假定使用ASCII兼容的二进制格式,不应应用于任意二进制数据。Note that all of the bytearray methods in this section do not operate in place, and instead produce new objects.请注意,本节中的所有bytearray方法都不是就地操作,而是生成新对象。
-
bytes.
capitalize
()¶ -
bytearray.
capitalize
()¶ Return a copy of the sequence with each byte interpreted as an ASCII character, and the first byte capitalized and the rest lowercased.返回序列的副本,每个字节解释为ASCII字符,第一个字节大写,其余字节小写。Non-ASCII byte values are passed through unchanged.非ASCII字节值会原封不动地传递。Note
The bytearray version of this method does not operate in place - it always produces a new object, even if no changes were made.此方法的bytearray版本无法原地操作:它始终生成一个新对象,即使没有进行任何更改。
-
bytes.
expandtabs
(tabsize=8)¶ -
bytearray.
expandtabs
(tabsize=8)¶ Return a copy of the sequence where all ASCII tab characters are replaced by one or more ASCII spaces, depending on the current column and the given tab size.根据当前列和给定的制表符大小,返回所有ASCII制表符被一个或多个ASCII空格替换的序列副本。Tab positions occur every tabsize bytes (default is 8, giving tab positions at columns 0, 8, 16 and so on).制表符位置每tabsize字节出现一次(默认值为8,在第0、8、16列等处给出制表符位置)。To expand the sequence, the current column is set to zero and the sequence is examined byte by byte.要扩展序列,请将当前列设置为零,然后逐字节检查序列。If the byte is an ASCII tab character (如果字节是ASCII制表符(b'\t'
), one or more space characters are inserted in the result until the current column is equal to the next tab position.b'\t'
),则在结果中插入一个或多个空格字符,直到当前列等于下一个制表符位置。(The tab character itself is not copied.)(不会复制制表符本身。)If the current byte is an ASCII newline (如果当前字节是ASCII换行符(b'\n'
) or carriage return (b'\r'
), it is copied and the current column is reset to zero.b'\n'
)或回车符(b'\r'
),则复制该字节,并将当前列重置为零。Any other byte value is copied unchanged and the current column is incremented by one regardless of how the byte value is represented when printed:任何其他字节值都会原封不动地复制,并且当前列将递增1,无论打印时字节值是如何表示的:>>> b'01\t012\t0123\t01234'.expandtabs()
b'01 012 0123 01234'
>>> b'01\t012\t0123\t01234'.expandtabs(4)
b'01 012 0123 01234'Note
The bytearray version of this method does not operate in place - it always produces a new object, even if no changes were made.此方法的bytearray版本无法原地操作:它始终生成一个新对象,即使没有进行任何更改。
-
bytes.
isalnum
()¶ -
bytearray.
isalnum
()¶ Return如果序列中的所有字节都是字母ASCII字符或ASCII十进制数字且序列不为空,则返回True
if all bytes in the sequence are alphabetical ASCII characters or ASCII decimal digits and the sequence is not empty,False
otherwise.True
,否则返回False
。Alphabetic ASCII characters are those byte values in the sequence字母ASCII字符是序列b'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
.b'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
中的字节值。ASCII decimal digits are those byte values in the sequenceASCII十进制数字是序列b'0123456789'
.b'0123456789'
中的字节值。For example:例如:>>> b'ABCabc1'.isalnum()
True
>>> b'ABC abc1'.isalnum()
False
-
bytes.
isalpha
()¶ -
bytearray.
isalpha
()¶ Return如果序列中的所有字节都是字母ASCII字符且序列不为空,则返回True
if all bytes in the sequence are alphabetic ASCII characters and the sequence is not empty,False
otherwise.True
,否则返回False
。Alphabetic ASCII characters are those byte values in the sequence字母ASCII字符是序列b'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
.b'abcdefghijklmnopqrstuvxyzabcdefghijklmnopqrstuvxyz'
中的字节值。For example:例如:>>> b'ABCabc'.isalpha()
True
>>> b'ABCabc1'.isalpha()
False
-
bytes.
isascii
()¶ -
bytearray.
isascii
()¶ Return如果序列为空或序列中的所有字节都是ASCII,则返回True
if the sequence is empty or all bytes in the sequence are ASCII,False
otherwise.True
,否则返回False
。ASCII bytes are in the range 0-0x7F.ASCII字节在0-0x7F范围内。New in version 3.7.版本3.7中新增。
-
bytes.
isdigit
()¶ -
bytearray.
isdigit
()¶ Return如果序列中的所有字节都是ASCII十进制数字且序列不为空,则返回True
if all bytes in the sequence are ASCII decimal digits and the sequence is not empty,False
otherwise.True
,否则返回False
。ASCII decimal digits are those byte values in the sequenceASCII十进制数字是序列b'0123456789'
.b'0123456789'
中的字节值。For example:例如:>>> b'1234'.isdigit()
True
>>> b'1.23'.isdigit()
False
-
bytes.
islower
()¶ -
bytearray.
islower
()¶ Return如果序列中至少有一个小写ASCII字符且没有大写ASCII字符,则返回True
if there is at least one lowercase ASCII character in the sequence and no uppercase ASCII characters,False
otherwise.True
,否则返回False
。For example:例如:>>> b'hello world'.islower()
True
>>> b'Hello world'.islower()
FalseLowercase ASCII characters are those byte values in the sequence小写ASCII字符是序列b'abcdefghijklmnopqrstuvwxyz'
.b'abcdefghijklmnopqrstuvwxyz'
中的字节值。Uppercase ASCII characters are those byte values in the sequence大写ASCII字符是序列b'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
.b'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
中的字节值。
-
bytes.
isspace
()¶ -
bytearray.
isspace
()¶ Return如果序列中的所有字节都是ASCII空格且序列不为空,则返回True
if all bytes in the sequence are ASCII whitespace and the sequence is not empty,False
otherwise.True
,否则返回False
。ASCII whitespace characters are those byte values in the sequenceASCII空白字符是序列b' \t\n\r\x0b\f'
(space, tab, newline, carriage return, vertical tab, form feed).b' \t\n\r\x0b\f'
(空格、制表符、换行符、回车符、垂直制表符、换行符)中的字节值。
-
bytes.
istitle
()¶ -
bytearray.
istitle
()¶ Return如果序列为ASCII titlecase且序列不为空,则返回True
if the sequence is ASCII titlecase and the sequence is not empty,False
otherwise.True
,否则返回False
。See有关“titlecase”定义的更多详细信息,请参阅bytes.title()
for more details on the definition of “titlecase”.bytes.title()
。For example:例如:>>> b'Hello World'.istitle()
True
>>> b'Hello world'.istitle()
False
-
bytes.
isupper
()¶ -
bytearray.
isupper
()¶ Return如果序列中至少有一个大写字母ASCII字符且没有小写ASCII字符,则返回True
if there is at least one uppercase alphabetic ASCII character in the sequence and no lowercase ASCII characters,False
otherwise.True
,否则返回False
。For example:例如:>>> b'HELLO WORLD'.isupper()
True
>>> b'Hello world'.isupper()
FalseLowercase ASCII characters are those byte values in the sequence小写ASCII字符是序列b'abcdefghijklmnopqrstuvwxyz'
.b'abcdefghijklmnopqrstuvwxyz'
中的字节值。Uppercase ASCII characters are those byte values in the sequence大写ASCII字符是序列b'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
.b'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
中的字节值。
-
bytes.
lower
()¶ -
bytearray.
lower
()¶ Return a copy of the sequence with all the uppercase ASCII characters converted to their corresponding lowercase counterpart.返回序列的副本,将所有大写ASCII字符转换为对应的小写字符。For example:例如:>>> b'Hello World'.lower()
b'hello world'Lowercase ASCII characters are those byte values in the sequence小写ASCII字符是序列b'abcdefghijklmnopqrstuvwxyz'
.b'abcdefghijklmnopqrstuvwxyz'
中的字节值。Uppercase ASCII characters are those byte values in the sequence大写ASCII字符是序列b'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
.b'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
中的字节值。Note
The bytearray version of this method does not operate in place - it always produces a new object, even if no changes were made.此方法的bytearray版本无法原地操作:它始终生成一个新对象,即使没有进行任何更改。
-
bytes.
splitlines
(keepends=False)¶ -
bytearray.
splitlines
(keepends=False)¶ Return a list of the lines in the binary sequence, breaking at ASCII line boundaries.返回二进制序列中的行列表,在ASCII行边界处断开。This method uses the universal newlines approach to splitting lines.此方法使用通用换行方法拆分行。Line breaks are not included in the resulting list unless keepends is given and true.除非给定了keepends且为true
,否则换行符不包括在结果列表中。For example:例如:>>> b'ab c\n\nde fg\rkl\r\n'.splitlines()
[b'ab c', b'', b'de fg', b'kl']
>>> b'ab c\n\nde fg\rkl\r\n'.splitlines(keepends=True)
[b'ab c\n', b'\n', b'de fg\r', b'kl\r\n']Unlike与给定分隔符字符串sep时的split()
when a delimiter string sep is given, this method returns an empty list for the empty string, and a terminal line break does not result in an extra line:split()
不同,此方法为空字符串返回一个空列表,并且换行符不会产生额外的行:>>> b"".split(b'\n'), b"Two lines\n".split(b'\n')
([b''], [b'Two lines', b''])
>>> b"".splitlines(), b"One line\n".splitlines()
([], [b'One line'])
-
bytes.
swapcase
()¶ -
bytearray.
swapcase
()¶ Return a copy of the sequence with all the lowercase ASCII characters converted to their corresponding uppercase counterpart and vice-versa.返回序列的副本,将所有小写ASCII字符转换为对应的大写字符,反之亦然。For example:例如:>>> b'Hello World'.swapcase()
b'hELLO wORLD'Lowercase ASCII characters are those byte values in the sequence小写ASCII字符是序列b'abcdefghijklmnopqrstuvwxyz'
.b'abcdefghijklmnopqrstuvwxyz'
中的字节值。Uppercase ASCII characters are those byte values in the sequence大写ASCII字符是序列b'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
.b'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
中的字节值。Unlike与str.swapcase()
, it is always the case thatbin.swapcase().swapcase() == bin
for the binary versions.str.swapcase()
不同,二进制版本的情况总是bin.swapcase().swapcase() == bin
。Case conversions are symmetrical in ASCII, even though that is not generally true for arbitrary Unicode code points.大小写转换在ASCII中是对称的,即使对于任意Unicode代码点通常不是这样。Note
The bytearray version of this method does not operate in place - it always produces a new object, even if no changes were made.此方法的bytearray版本无法原地操作:它始终生成一个新对象,即使没有进行任何更改。
-
bytes.
title
()¶ -
bytearray.
title
()¶ Return a titlecased version of the binary sequence where words start with an uppercase ASCII character and the remaining characters are lowercase.返回二进制序列的基于标题的版本,其中单词以大写ASCII字符开头,其余字符为小写。Uncased byte values are left unmodified.未加密的字节值保持不变。For example:例如:>>> b'Hello world'.title()
b'Hello World'Lowercase ASCII characters are those byte values in the sequence小写ASCII字符是序列b'abcdefghijklmnopqrstuvwxyz'
.b'abcdefghijklmnopqrstuvwxyz'
中的字节值。Uppercase ASCII characters are those byte values in the sequence大写ASCII字符是序列b'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
.b'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
中的字节值。All other byte values are uncased.所有其他字节值均未加密。The algorithm uses a simple language-independent definition of a word as groups of consecutive letters.该算法使用一个简单的独立于语言的定义,将一个单词定义为一组连续的字母。The definition works in many contexts but it means that apostrophes in contractions and possessives form word boundaries, which may not be the desired result:这个定义在许多上下文中都适用,但它意味着缩略语和所有格中的撇号形成了单词边界,这可能不是期望的结果:>>> b"they're bill's friends from the UK".title()
b"They'Re Bill'S Friends From The Uk"A workaround for apostrophes can be constructed using regular expressions:可以使用正则表达式构造撇号的变通方法:>>> import re
>>> def titlecase(s):
... return re.sub(rb"[A-Za-z]+('[A-Za-z]+)?",
... lambda mo: mo.group(0)[0:1].upper() +
... mo.group(0)[1:].lower(),
... s)
...
>>> titlecase(b"they're bill's friends.")
b"They're Bill's Friends."Note
The bytearray version of this method does not operate in place - it always produces a new object, even if no changes were made.此方法的bytearray版本无法原地操作:它始终生成一个新对象,即使没有进行任何更改。
-
bytes.
upper
()¶ -
bytearray.
upper
()¶ Return a copy of the sequence with all the lowercase ASCII characters converted to their corresponding uppercase counterpart.返回序列的副本,将所有小写ASCII字符转换为对应的大写字符。For example:例如:>>> b'Hello World'.upper()
b'HELLO WORLD'Lowercase ASCII characters are those byte values in the sequence小写ASCII字符是序列b'abcdefghijklmnopqrstuvwxyz'
.b'abcdefghijklmnopqrstuvwxyz'
中的字节值。Uppercase ASCII characters are those byte values in the sequence大写ASCII字符是序列b'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
.b'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
中的字节值。Note
The bytearray version of this method does not operate in place - it always produces a new object, even if no changes were made.此方法的bytearray版本无法原地操作:它始终生成一个新对象,即使没有进行任何更改。
-
bytes.
zfill
(width)¶ -
bytearray.
zfill
(width)¶ Return a copy of the sequence left filled with ASCII返回由ASCIIb'0'
digits to make a sequence of length width.b'0'
数字填充的序列副本,以生成长度width序列。A leading sign prefix (前导符号前缀(b'+'
/b'-'
) is handled by inserting the padding after the sign character rather than before.b'+'
/b'-'
)是通过在符号字符之后而不是之前插入填充来处理的。For对于bytes
objects, the original sequence is returned if width is less than or equal tolen(seq)
.bytes
对象,如果width小于或等于len(seq)
,则返回原始序列。For example:例如:>>> b"42".zfill(5)
b'00042'
>>> b"-42".zfill(5)
b'-0042'Note
The bytearray version of this method does not operate in place - it always produces a new object, even if no changes were made.此方法的bytearray版本无法原地操作:它始终生成一个新对象,即使没有进行任何更改。
printf
-style Bytes Formatting:样式化字节格式¶
Note
The formatting operations described here exhibit a variety of quirks that lead to a number of common errors (such as failing to display tuples and dictionaries correctly). If the value being printed may be a tuple or dictionary, wrap it in a tuple.这里描述的格式化操作表现出各种各样的怪癖,这些怪癖会导致许多常见错误(例如无法正确显示元组和字典)。如果要打印的值可能是元组或字典,请将其包装在元组中。
Bytes objects (字节对象(bytes
/bytearray
) have one unique built-in operation: the %
operator (modulo). bytes
/bytearray
)有一个唯一的内置操作:%
运算符(模)。This is also known as the bytes formatting or interpolation operator. 这也称为字节格式化或插值运算符。Given 给定format % values
(where format is a bytes object), %
conversion specifications in format are replaced with zero or more elements of values. format % values
(其中format是字节对象),format中的%
转换规范将替换为零个或多个值元素。The effect is similar to using the 其效果类似于在C语言中使用sprintf()
in the C language.sprintf()
。
If format requires a single argument, values may be a single non-tuple object. 如果format需要单个参数,则值可以是单个非元组对象。5 Otherwise, values must be a tuple with exactly the number of items specified by the format bytes object, or a single mapping object (for example, a dictionary).否则,values必须是一个元组,其项数必须与format bytes对象或单个映射对象(例如,字典)指定的项数完全相同。
A conversion specifier contains two or more characters and has the following components, which must occur in this order:转换说明符包含两个或多个字符,并具有以下组件,这些组件必须按以下顺序出现:
The'%'
character, which marks the start of the specifier.'%'
字符,它标记说明符的开头。Mapping key (optional), consisting of a parenthesised sequence of characters (for example,映射键(可选),由带括号的字符序列组成(例如,(somename)
).(somename)
)。Conversion flags (optional), which affect the result of some conversion types.转换标志(可选),影响某些转换类型的结果。Minimum field width (optional).最小字段宽度(可选)。If specified as an如果指定为'*'
(asterisk), the actual width is read from the next element of the tuple in values, and the object to convert comes after the minimum field width and optional precision.'*'
(星号),则从values中元组的下一个元素读取实际宽度,要转换的对象位于最小字段宽度和可选精度之后。Precision (optional), given as a精度(可选),以'.'
(dot) followed by the precision.'.'
表示(点)后跟精度。If specified as如果指定为'*'
(an asterisk), the actual precision is read from the next element of the tuple in values, and the value to convert comes after the precision.'.'
(星号),则从values中元组的下一个元素读取实际精度,要转换的值位于精度之后。Length modifier (optional).长度修改器(可选)。Conversion type.转换类型。
When the right argument is a dictionary (or other mapping type), then the formats in the bytes object must include a parenthesised mapping key into that dictionary inserted immediately after the 当右参数是字典(或其他映射类型)时,字节对象中的格式必须在紧跟'%'
character. '%'
字符之后插入的字典中包含带括号的映射键。The mapping key selects the value to be formatted from the mapping. 映射键从映射中选择要格式化的值。For example:例如:
>>> print(b'%(language)s has %(number)03d quote types.' %
... {b'language': b"Python", b"number": 2})
b'Python has 002 quote types.'
In this case no 在这种情况下,格式中可能不会出现*
specifiers may occur in a format (since they require a sequential parameter list).*
说明符(因为它们需要顺序参数列表)。
The conversion flag characters are:转换标志字符为:
|
|
---|---|
|
|
|
|
|
|
|
|
|
|
A length modifier (长度修饰符(h
, l
, or L
) may be present, but is ignored as it is not necessary for Python – so e.g. %ld
is identical to %d
.h
、l
或L
)可能存在,但会被忽略,因为Python不需要它-因此,例如%ld
与%d
相同。
The conversion types are:转换类型包括:
|
|
Notes |
---|---|---|
|
|
|
|
|
|
|
|
(1) |
|
|
(8) |
|
|
(2) |
|
|
(2) |
|
|
(3) |
|
|
(3) |
|
|
(3) |
|
|
(3) |
|
|
(4) |
|
|
(4) |
|
|
|
|
|
(5) |
|
|
(6) |
|
|
(5) |
|
|
(7) |
|
|
Notes:
The alternate form causes a leading octal specifier (替代形式导致在第一个数字之前插入前导八进制说明符('0o'
) to be inserted before the first digit.'0o'
)。The alternate form causes a leading备用格式导致在第一个数字之前插入前导'0x'
or'0X'
(depending on whether the'x'
or'X'
format was used) to be inserted before the first digit.'0x'
或'0X'
(取决于使用的是'x'
或'X'
格式)。The alternate form causes the result to always contain a decimal point, even if no digits follow it.替代形式使结果始终包含小数点,即使其后没有数字。The precision determines the number of digits after the decimal point and defaults to 6.精度确定小数点后的位数,默认为6。The alternate form causes the result to always contain a decimal point, and trailing zeroes are not removed as they would otherwise be.替代形式会导致结果始终包含小数点,并且不会像其他形式那样删除尾随的零。The precision determines the number of significant digits before and after the decimal point and defaults to 6.精度确定小数点前后的有效位数,默认为6。If precision is如果精度为N
, the output is truncated toN
characters.N
,则输出将截断为N个字符。b'%s'
is deprecated, but will not be removed during the 3.x series.已弃用,但不会在3.x系列中删除。b'%r'
is deprecated, but will not be removed during the 3.x series.已弃用,但不会在3.x系列中删除。
Note
The bytearray version of this method does not operate in place - it always produces a new object, even if no changes were made.此方法的bytearray版本无法原地操作:它始终生成一个新对象,即使没有进行任何更改。
See also
PEP 461 - Adding % formatting to bytes and bytearray将%格式添加到字节和字节数组
New in version 3.5.版本3.5中新增。
Memory Views内存视图¶
memoryview
objects allow Python code to access the internal data of an object that supports the buffer protocol without copying.对象允许Python代码访问支持缓冲区协议的对象的内部数据,而无需复制。
-
class
memoryview
(object)¶ Create a创建引用object的memoryview
that references object. object must support the buffer protocol.memoryview
。object必须支持缓冲区协议。Built-in objects that support the buffer protocol include支持缓冲区协议的内置对象包括bytes
andbytearray
.bytes
和bytearray
。Amemoryview
has the notion of an element, which is the atomic memory unit handled by the originating object.memoryview
具有元素的概念,元素是由原始对象处理的原子内存单元。For many simple types such as对于许多简单类型(如bytes
andbytearray
, an element is a single byte, but other types such asarray.array
may have bigger elements.bytes
和bytearray
),元素是单个字节,但其他类型(如array.array
)可能有更大的元素。len(view)
is equal to the length of等于tolist
.tolist
的长度。If如果view.ndim = 0
, the length is 1.view.ndim = 0
,则长度为1。If如果view.ndim = 1
, the length is equal to the number of elements in the view.view.ndim = 1
,则长度等于视图中的图元数。For higher dimensions, the length is equal to the length of the nested list representation of the view.对于较高维度,长度等于视图的嵌套列表表示形式的长度。Theitemsize
attribute will give you the number of bytes in a single element.itemsize
属性将为您提供单个元素中的字节数。Amemoryview
supports slicing and indexing to expose its data.memoryview
支持切片和索引以公开其数据。One-dimensional slicing will result in a subview:一维切片将生成子视图:>>> v = memoryview(b'abcefg')
>>> v[1]
98
>>> v[-1]
103
>>> v[1:4]
<memory at 0x7f3ddc9f4350>
>>> bytes(v[1:4])
b'bce'If如果format
is one of the native format specifiers from thestruct
module, indexing with an integer or a tuple of integers is also supported and returns a single element with the correct type.format
是struct
模块中的本机格式说明符之一,则还支持使用整数或整数元组进行索引,并返回具有正确类型的单个element。One-dimensional memoryviews can be indexed with an integer or a one-integer tuple.一维MemoryView可以使用整数或整数元组进行索引。Multi-dimensional memoryviews can be indexed with tuples of exactly ndim integers where ndim is the number of dimensions.多维memoryview可以使用ndim整数的元组进行索引,其中ndim是维度数。Zero-dimensional memoryviews can be indexed with the empty tuple.零维MemoryView可以使用空元组进行索引。Here is an example with a non-byte format:以下是一个非字节格式的示例:>>> import array
>>> a = array.array('l', [-11111111, 22222222, -33333333, 44444444])
>>> m = memoryview(a)
>>> m[0]
-11111111
>>> m[-1]
44444444
>>> m[::2].tolist()
[-11111111, -33333333]If the underlying object is writable, the memoryview supports one-dimensional slice assignment.如果基础对象是可写的,则memoryview支持一维切片分配。Resizing is not allowed:不允许调整大小:>>> data = bytearray(b'abcefg')
>>> v = memoryview(data)
>>> v.readonly
False
>>> v[0] = ord(b'z')
>>> data
bytearray(b'zbcefg')
>>> v[1:4] = b'123'
>>> data
bytearray(b'z123fg')
>>> v[2:3] = b'spam'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: memoryview assignment: lvalue and rvalue have different structures
>>> v[2:6] = b'spam'
>>> data
bytearray(b'z1spam')One-dimensional memoryviews of hashable (read-only) types with formats ‘B’, ‘b’ or ‘c’ are also hashable.格式为“B”、“B”或“c”的可哈希(只读)类型的一维内存视图也是可哈希的。The hash is defined as散列定义为hash(m) == hash(m.tobytes())
:hash(m) == hash(m.tobytes())
:>>> v = memoryview(b'abcefg')
>>> hash(v) == hash(b'abcefg')
True
>>> hash(v[2:4]) == hash(b'ce')
True
>>> hash(v[::-2]) == hash(b'abcefg'[::-2])
TrueChanged in version 3.3:在版本3.3中更改:One-dimensional memoryviews can now be sliced.现在可以对一维memoryview进行切片。One-dimensional memoryviews with formats ‘B’, ‘b’ or ‘c’ are now hashable.格式为“B”、“B”或“c”的一维内存视图现在可以散列。Changed in version 3.4:在版本3.4中更改:memoryview is now registered automatically withmemoryview现在自动注册到collections.abc.Sequence
collections.abc.Sequence
Changed in version 3.5:在版本3.5中更改:memoryviews can now be indexed with tuple of integers.memoryview现在可以使用整数元组进行索引。memoryview
has several methods:有几种方法:-
__eq__
(exporter)¶ A memoryview and a PEP 3118 exporter are equal if their shapes are equivalent and if all corresponding values are equal when the operands’ respective format codes are interpreted using如果使用struct
syntax.struct
语法解释操作数各自的格式代码时,memoryview和PEP 3118导出器的形状相等,并且所有相应的值相等,则它们相等。For the subset of对于struct
format strings currently supported bytolist()
,v
andw
are equal ifv.tolist() == w.tolist()
:tolist()
当前支持的struct
格式字符串子集,如果v.tolist() == w.tolist()
,则v
和w
相等:>>> import array
>>> a = array.array('I', [1, 2, 3, 4, 5])
>>> b = array.array('d', [1.0, 2.0, 3.0, 4.0, 5.0])
>>> c = array.array('b', [5, 3, 1])
>>> x = memoryview(a)
>>> y = memoryview(b)
>>> x == a == y == b
True
>>> x.tolist() == a.tolist() == y.tolist() == b.tolist()
True
>>> z = y[::-2]
>>> z == c
True
>>> z.tolist() == c.tolist()
TrueIf either format string is not supported by the如果struct
module, then the objects will always compare as unequal (even if the format strings and buffer contents are identical):struct
模块不支持任何一种格式字符串,则对象将始终比较为不相等(即使格式字符串和缓冲区内容相同):>>> from ctypes import BigEndianStructure, c_long
>>> class BEPoint(BigEndianStructure):
... _fields_ = [("x", c_long), ("y", c_long)]
...
>>> point = BEPoint(100, 200)
>>> a = memoryview(point)
>>> b = memoryview(point)
>>> a == point
False
>>> a == b
FalseNote that, as with floating point numbers,请注意,与浮点数一样,对于memoryview对象,v is w
does not implyv == w
for memoryview objects.v is w
并不意味着v == w
。Changed in version 3.3:在版本3.3中更改:Previous versions compared the raw memory disregarding the item format and the logical array structure.以前的版本比较了原始内存,忽略了项格式和逻辑阵列结构。
-
tobytes
(order=None)¶ Return the data in the buffer as a bytestring.将缓冲区中的数据作为bytestring返回。This is equivalent to calling the这相当于在memoryview上调用bytes
constructor on the memoryview.bytes
构造函数。>>> m = memoryview(b"abc")
>>> m.tobytes()
b'abc'
>>> bytes(m)
b'abc'For non-contiguous arrays the result is equal to the flattened list representation with all elements converted to bytes.对于非连续数组,结果等于将所有元素转换为字节的展平列表表示。tobytes()
supports all format strings, including those that are not in支持所有格式字符串,包括那些不在struct
module syntax.struct
模块语法中的字符串。New in version 3.8:版本3.8中新增:ordercan be {‘C’, ‘F’, ‘A’}.可以是{‘C’, ‘F’, ‘A’}。When order is ‘C’ or ‘F’, the data of the original array is converted to C or Fortran order.当order为‘C’或‘F’时,原始数组的数据将转换为C或Fortran顺序。For contiguous views, ‘A’ returns an exact copy of the physical memory.对于连续视图,“A”返回物理内存的精确副本。In particular, in-memory Fortran order is preserved.特别是,内存中的Fortran顺序得以保留。For non-contiguous views, the data is converted to C first.对于非连续视图,首先将数据转换为C。order=None is the same as order=’C’.order=None与order=‘C’相同。
-
hex
([sep[, bytes_per_sep]])¶ Return a string object containing two hexadecimal digits for each byte in the buffer.返回一个字符串对象,该对象包含缓冲区中每个字节的两个十六进制数字。>>> m = memoryview(b"abc")
>>> m.hex()
'616263'New in version 3.5.版本3.5中新增。Changed in version 3.8:版本3.8中更改:Similar to与bytes.hex()
,memoryview.hex()
now supports optional sep and bytes_per_sep parameters to insert separators between bytes in the hex output.bytes.hex()
类似,memoryview.hex()
现在支持可选的sep和bytes_per_sep参数,以便在十六进制输出中的字节之间插入分隔符。
-
tolist
()¶ Return the data in the buffer as a list of elements.以元素列表的形式返回缓冲区中的数据。>>> memoryview(b'abc').tolist()
[97, 98, 99]
>>> import array
>>> a = array.array('d', [1.1, 2.2, 3.3])
>>> m = memoryview(a)
>>> m.tolist()
[1.1, 2.2, 3.3]
-
toreadonly
()¶ Return a readonly version of the memoryview object.返回memoryview对象的只读版本。The original memoryview object is unchanged.原始memoryview对象不变。>>> m = memoryview(bytearray(b'abc'))
>>> mm = m.toreadonly()
>>> mm.tolist()
[89, 98, 99]
>>> mm[0] = 42
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: cannot modify read-only memory
>>> m[0] = 43
>>> mm.tolist()
[43, 98, 99]New in version 3.8.版本3.8中新增。
-
release
()¶ Release the underlying buffer exposed by the memoryview object.释放memoryview对象公开的底层缓冲区。Many objects take special actions when a view is held on them (for example, a许多对象在持有视图时会执行特殊操作(例如,bytearray
would temporarily forbid resizing); therefore, calling release() is handy to remove these restrictions (and free any dangling resources) as soon as possible.bytearray
会暂时禁止调整大小);因此,调用release()
可以很方便地尽快删除这些限制(并释放任何悬而未决的资源)。After this method has been called, any further operation on the view raises a调用此方法后,视图上的任何进一步操作都会引发ValueError
(exceptrelease()
itself which can be called multiple times):ValueError
(release()
本身除外,它可以被多次调用):>>> m = memoryview(b'abc')
>>> m.release()
>>> m[0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: operation forbidden on released memoryview objectThe context management protocol can be used for a similar effect, using the使用with
statement:with
语句,可以使用上下文管理协议实现类似的效果:>>> with memoryview(b'abc') as m:
... m[0]
...
97
>>> m[0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: operation forbidden on released memoryview objectNew in version 3.2.版本3.2中新增。
-
cast
(format[, shape])¶ Cast a memoryview to a new format or shape.将memoryview转换为新格式或形状。shape defaults toshape默认为[byte_length//new_itemsize]
, which means that the result view will be one-dimensional.[byte_length//new_itemsize]
,这意味着结果视图将是一维的。The return value is a new memoryview, but the buffer itself is not copied.返回值是一个新的memoryview,但不会复制缓冲区本身。Supported casts are 1D -> C-contiguous and C-contiguous -> 1D.支持的强制转换为1D->C-连续和C-连续->1D。The destination format is restricted to a single element native format in目标格式在struct
syntax.struct
语法中仅限于单个元素的本机格式。One of the formats must be a byte format (‘B’, ‘b’ or ‘c’).其中一种格式必须是字节格式(‘B’、‘b’或‘c’)。The byte length of the result must be the same as the original length.结果的字节长度必须与原始长度相同。Cast 1D/long to 1D/unsigned bytes:将1D/长字节强制转换为1D/无符号字节:>>> import array
>>> a = array.array('l', [1,2,3])
>>> x = memoryview(a)
>>> x.format
'l'
>>> x.itemsize
8
>>> len(x)
3
>>> x.nbytes
24
>>> y = x.cast('B')
>>> y.format
'B'
>>> y.itemsize
1
>>> len(y)
24
>>> y.nbytes
24Cast 1D/unsigned bytes to 1D/char:将1D/无符号字节强制转换为1D/字符:>>> b = bytearray(b'zyz')
>>> x = memoryview(b)
>>> x[0] = b'a'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: memoryview: invalid value for format "B"
>>> y = x.cast('c')
>>> y[0] = b'a'
>>> b
bytearray(b'ayz')Cast 1D/bytes to 3D/ints to 1D/signed char:将1D/字节转换为3D/整数转换为1D/有符号字符:>>> import struct
>>> buf = struct.pack("i"*12, *list(range(12)))
>>> x = memoryview(buf)
>>> y = x.cast('i', shape=[2,2,3])
>>> y.tolist()
[[[0, 1, 2], [3, 4, 5]], [[6, 7, 8], [9, 10, 11]]]
>>> y.format
'i'
>>> y.itemsize
4
>>> len(y)
2
>>> y.nbytes
48
>>> z = y.cast('b')
>>> z.format
'b'
>>> z.itemsize
1
>>> len(z)
48
>>> z.nbytes
48Cast 1D/unsigned long to 2D/unsigned long:将1D/无符号长转换为2D/无符号长:>>> buf = struct.pack("L"*6, *list(range(6)))
>>> x = memoryview(buf)
>>> y = x.cast('L', shape=[2,3])
>>> len(y)
2
>>> y.nbytes
48
>>> y.tolist()
[[0, 1, 2], [3, 4, 5]]New in version 3.3.版本3.3中新增。Changed in version 3.5:版本3.5中更改:The source format is no longer restricted when casting to a byte view.转换为字节视图时,源格式不再受限制。
There are also several readonly attributes available:还有几个只读属性可用:-
obj
¶ The underlying object of the memoryview:memoryview的底层对象:>>> b = bytearray(b'xyz')
>>> m = memoryview(b)
>>> m.obj is b
TrueNew in version 3.3.版本3.3中新增。
-
nbytes
¶ nbytes == product(shape) * itemsize == len(m.tobytes())
.This is the amount of space in bytes that the array would use in a contiguous representation.这是数组将在连续表示中使用的字节空间量。It is not necessarily equal to它不一定等于len(m)
:len(m)
:>>> import array
>>> a = array.array('i', [1,2,3,4,5])
>>> m = memoryview(a)
>>> len(m)
5
>>> m.nbytes
20
>>> y = m[::2]
>>> len(y)
3
>>> y.nbytes
12
>>> len(y.tobytes())
12Multi-dimensional arrays:多维数组:>>> import struct
>>> buf = struct.pack("d"*12, *[1.5*x for x in range(12)])
>>> x = memoryview(buf)
>>> y = x.cast('d', shape=[3,4])
>>> y.tolist()
[[0.0, 1.5, 3.0, 4.5], [6.0, 7.5, 9.0, 10.5], [12.0, 13.5, 15.0, 16.5]]
>>> len(y)
3
>>> y.nbytes
96New in version 3.3.版本3.3中新增。
-
readonly
¶ A bool indicating whether the memory is read only.指示内存是否为只读的布尔值。
-
format
¶ A string containing the format (in包含视图中每个元素的格式(struct
module style) for each element in the view.struct
模块样式)的字符串。A memoryview can be created from exporters with arbitrary format strings, but some methods (e.g.可以使用任意格式字符串从导出器创建memoryview,但某些方法(例如tolist()
) are restricted to native single element formats.tolist()
)仅限于本机单元素格式。Changed in version 3.3:版本3.3中更改:format格式'B'
is now handled according to the struct module syntax.'B'
现在根据结构模块语法进行处理。This means that这意味着memoryview(b'abc')[0] == b'abc'[0] == 97
.memoryview(b'abc')[0] == b'abc'[0] == 97
。
-
itemsize
¶ The size in bytes of each element of the memoryview:memoryview中每个元素的大小(以字节为单位):>>> import array, struct
>>> m = memoryview(array.array('H', [32000, 32001, 32002]))
>>> m.itemsize
2
>>> m[0]
32000
>>> struct.calcsize('H') == m.itemsize
True
-
ndim
¶ An integer indicating how many dimensions of a multi-dimensional array the memory represents.一个整数,指示内存表示多维数组的维数。
-
shape
¶ A tuple of integers the length of一个整数元组,ndim
giving the shape of the memory as an N-dimensional array.ndim
的长度表示内存的形状,为N维数组。Changed in version 3.3:版本3.3中更改:An empty tuple instead ofndim=0时,为空元组,而不是None
when ndim = 0.None
。
-
strides
¶ A tuple of integers the length of一个整数元组,即ndim
giving the size in bytes to access each element for each dimension of the array.ndim
的长度,给出访问数组每个维度的每个元素的字节大小。Changed in version 3.3:版本3.3中更改:An empty tuple instead ofndim=0时,为空元组,而不是None
when ndim = 0.None
。
-
suboffsets
¶ Used internally for PIL-style arrays.内部用于PIL样式数组。The value is informational only.该值仅供参考。
-
c_contiguous
¶ A bool indicating whether the memory is C-contiguous.一种布尔值,指示内存是否是C连续。New in version 3.3.版本3.3中新增。
-
f_contiguous
¶ A bool indicating whether the memory is Fortran contiguous.一种布尔值,指示内存是否是Fortran连续。New in version 3.3.版本3.3中新增。
-
contiguous
¶ A bool indicating whether the memory is contiguous.指示内存是否连续的布尔值。New in version 3.3.版本3.3中新增。
-
Set Types — 集类型:set
, frozenset
¶
A set object is an unordered collection of distinct hashable objects. set对象是不同哈希对象的无序集合。Common uses include membership testing, removing duplicates from a sequence, and computing mathematical operations such as intersection, union, difference, and symmetric difference. 常见用途包括成员身份测试、从序列中删除重复项,以及计算数学运算,如交集、并集、差分和对称差分。(For other containers see the built-in (有关其他容器,请参阅内置的dict
, list
, and tuple
classes, and the collections
module.)dict
、list
和tuple
类以及collections
模块。)
Like other collections, sets support 与其他集合一样,集支持x in set
, len(set)
, and for x in set
. x in set
、len(set)
和for x in set
。Being an unordered collection, sets do not record element position or order of insertion. 集是无序集合,不记录元素位置或插入顺序。Accordingly, sets do not support indexing, slicing, or other sequence-like behavior.因此,集不支持索引、切片或其他类似序列的行为。
There are currently two built-in set types, 当前有两种内置的集合类型,set
and frozenset
. set
和frozenset
。The set
type is mutable — the contents can be changed using methods like add()
and remove()
. set
类型是可变的-可以使用add()
和remove()
等方法更改内容。Since it is mutable, it has no hash value and cannot be used as either a dictionary key or as an element of another set. 因为它是可变的,所以它没有哈希值,不能用作字典键或另一个集合的元素。The frozenset
type is immutable and hashable — its contents cannot be altered after it is created; it can therefore be used as a dictionary key or as an element of another set.frozenset
类型是不可变和可散列的:创建后其内容不能更改;因此,它可以用作字典键或另一个集的元素。
Non-empty sets (not frozensets) can be created by placing a comma-separated list of elements within braces, for example: 非空集(非冻结集)可以通过在大括号中放置逗号分隔的元素列表来创建,例如:{'jack', 'sjoerd'}
, in addition to the set
constructor.{'jack', 'sjoerd'}
,以及set
构造函数。
The constructors for both classes work the same:两个类的构造函数的工作方式相同:
-
class
set
([iterable])¶ -
class
frozenset
([iterable])¶ Return a new set or frozenset object whose elements are taken from iterable.返回其元素取自iterable的新集或冻结集对象。The elements of a set must be hashable.集的元素必须是可散列的。To represent sets of sets, the inner sets must be要表示集的集,内部集必须是frozenset
objects.frozenset
对象。If iterable is not specified, a new empty set is returned.如果未指定iterable,则返回一个新的空集。Sets can be created by several means:可以通过多种方式创建集:Use a comma-separated list of elements within braces:在大括号内使用逗号分隔的元素列表:{'jack', 'sjoerd'}
Use a set comprehension:使用集推导:{c for c in 'abracadabra' if c not in 'abc'}
Use the type constructor:使用类型构造函数:set()
,、set('foobar')
,、set(['a', 'b', 'foo'])
Instances ofset
andfrozenset
provide the following operations:set
和frozenset
的实例提供以下操作:-
len(s)
Return the number of elements in set s (cardinality of s).返回集s中的元素数(s的基数)。
-
x in s
Test x for membership in s.测试x是否为s中的成员。
-
x not in s
Test x for non-membership in s.测试x是否为s中的非成员。
-
isdisjoint
(other)¶ Return如果集没有与other相同的元素,则返回True
if the set has no elements in common with other.True
。Sets are disjoint if and only if their intersection is the empty set.当且仅当集的交集为空集时,集才是不相交的。
-
issubset
(other)¶ -
set <= other
Test whether every element in the set is in other.测试集中的每个元素是否在other中。
-
set < other
Test whether the set is a proper subset of other, that is,测试集是否是other的正确子集,即set <= other and set != other
.set <= other and set != other
-
issuperset
(other)¶ -
set >= other
Test whether every element in other is in the set.测试other中的每个元素是否都在集中。
-
set > other
Test whether the set is a proper superset of other, that is,测试集是否是other的正确超集,即set >= other and set != other
.set >= other and set != other
-
union
(*others)¶ -
set | other | ...
Return a new set with elements from the set and all others.返回一个新的集,其中包含该集和所有其他集中的元素。
-
intersection
(*others)¶ -
set & other & ...
Return a new set with elements common to the set and all others.返回一个新的集,其中包含该集和所有其他集共有的元素。
-
difference
(*others)¶ -
set - other - ...
Return a new set with elements in the set that are not in the others.返回一个新的集,该集中的元素不在其他集中。
-
symmetric_difference
(other)¶ -
set ^ other
Return a new set with elements in either the set or other but not both.返回一个新集,其中包含集中的元素或other中的元素,但不能同时包含在这两个集中。
-
copy
()¶ Return a shallow copy of the set.返回该集的浅拷贝。
Note, the non-operator versions of注意,union()
,intersection()
,difference()
,symmetric_difference()
,issubset()
, andissuperset()
methods will accept any iterable as an argument.union()
、intersection()
、difference()
、symmetric_difference()
、issubset()
和issuperset()
方法的非运算符版本将接受任何iterable作为参数。In contrast, their operator based counterparts require their arguments to be sets.相反,它们基于运算符的对应项要求它们的参数为集。This precludes error-prone constructions like这就排除了像set('abc') & 'cbs'
in favor of the more readableset('abc').intersection('cbs')
.set('abc') & 'cbs'
这样容易出错的构造,从而有利于更具可读性的集set('abc').intersection('cbs')
。Bothset
andfrozenset
support set to set comparisons.set
和frozenset
都支持集对集比较。Two sets are equal if and only if every element of each set is contained in the other (each is a subset of the other).当且仅当每个集的每个元素都包含在另一个集中(每个元素都是另一个集的子集)时,两个集才相等。A set is less than another set if and only if the first set is a proper subset of the second set (is a subset, but is not equal).当且仅当第一个集是第二个集的适当子集(是子集,但不相等)时,一个集小于另一个集。A set is greater than another set if and only if the first set is a proper superset of the second set (is a superset, but is not equal).当且仅当第一个集是第二个集的正确超集(是超集,但不相等)时,一个集大于另一个集。Instances ofset
are compared to instances offrozenset
based on their members.set
实例根据其成员与frozenset
实例进行比较。For example,例如,set('abc') == frozenset('abc')
returnsTrue
and so doesset('abc') in set([frozenset('abc')])
.set('abc') == frozenset('abc')
返回True
,set('abc') in set([frozenset('abc')])
也返回True
。The subset and equality comparisons do not generalize to a total ordering function.子集和等式比较不能推广到全序函数。For example, any two nonempty disjoint sets are not equal and are not subsets of each other, so all of the following return例如,任何两个非空不相交集都不相等,也不是彼此的子集,因此以下所有结果都返回False
:a<b
,a==b
, ora>b
.False
:a<b
,a==b
,或a>b
。Since sets only define partial ordering (subset relationships), the output of the由于集仅定义偏序(子集关系),因此对于集列表,list.sort()
method is undefined for lists of sets.list.sort()
方法的输出未定义。Set elements, like dictionary keys, must be hashable.集元素(如字典键)必须是可哈希的。Binary operations that mix将set
instances withfrozenset
return the type of the first operand.set
实例与frozenset
混合的二进制操作返回第一个操作数的类型。For example:例如:frozenset('ab') | set('bc')
returns an instance offrozenset
.frozenset('ab') | set('bc')
返回frozenset
的实例。The following table lists operations available for下表列出了可用于set
that do not apply to immutable instances offrozenset
:set
的操作,这些操作不适用于frozenset
的不可变实例:-
update
(*others)¶ -
set |= other | ...
Update the set, adding elements from all others.更新集,添加所有other中的元素。
-
intersection_update
(*others)¶ -
set &= other & ...
Update the set, keeping only elements found in it and all others.更新集,仅保留其中找到的元素和所有其他元素。
-
difference_update
(*others)¶ -
set -= other | ...
Update the set, removing elements found in others.更新集,删除在其他集中找到的元素。
-
symmetric_difference_update
(other)¶ -
set ^= other
Update the set, keeping only elements found in either set, but not in both.更新集,只保留在任一集中找到的元素,但不能同时在两个集中找到。
-
add
(elem)¶ Add element elem to the set.将元素elem添加到集中。
-
remove
(elem)¶ Remove element elem from the set.从集中删除元素elem。Raises如果集中不包含elem,则引发KeyError
if elem is not contained in the set.KeyError
。
-
discard
(elem)¶ Remove element elem from the set if it is present.从集中删除元素elem(如果存在)。
-
pop
()¶ Remove and return an arbitrary element from the set.从集中删除并返回任意元素。Raises如果集为空,则引发KeyError
if the set is empty.KeyError
。
-
clear
()¶ Remove all elements from the set.从集中删除所有元素。
Note, the non-operator versions of the注意,update()
,intersection_update()
,difference_update()
, andsymmetric_difference_update()
methods will accept any iterable as an argument.update()
、intersection_update()
、difference_update()
和symmetric_difference_update()
方法的非运算符版本将接受任何iterable作为参数。Note, the elem argument to the
__contains__()
,remove()
, anddiscard()
methods may be a set.To support searching for an equivalent frozenset, a temporary one is created from elem.为了支持搜索等效冻结集,将从elem创建一个临时冻结集。
Mapping Types — 映射类型:dict
¶
A mapping object maps hashable values to arbitrary objects. mapping对象将可哈希值映射到任意对象。Mappings are mutable objects. 映射是可变对象。There is currently only one standard mapping type, the dictionary. 目前只有一种标准映射类型,即字典。(For other containers see the built-in (有关其他容器,请参阅内置的list
, set
, and tuple
classes, and the collections
module.)list
、set
和tuple
类以及collections
模块。)
A dictionary’s keys are almost arbitrary values. 字典的键几乎是任意值。Values that are not hashable, that is, values containing lists, dictionaries or other mutable types (that are compared by value rather than by object identity) may not be used as keys. 不可散列的值,即包含列表、字典或其他可变类型(通过值而不是对象标识进行比较)的值,不能用作键。Numeric types used for keys obey the normal rules for numeric comparison: if two numbers compare equal (such as 用于键的数字类型遵循数字比较的常规规则:如果两个数字比较相等(例如1
and 1.0
) then they can be used interchangeably to index the same dictionary entry. 1
和1.0
),则可以互换使用它们来索引相同的字典条目。(Note however, that since computers store floating-point numbers as approximations it is usually unwise to use them as dictionary keys.)(但请注意,由于计算机将浮点数存储为近似值,因此将它们用作字典键通常是不明智的。)
-
class
dict
(**kwargs)¶ -
class
dict
(mapping, **kwargs) -
class
dict
(iterable, **kwargs) Return a new dictionary initialized from an optional positional argument and a possibly empty set of keyword arguments.返回从可选位置参数和可能为空的关键字参数集初始化的新词典。Dictionaries can be created by several means:可以通过多种方式创建词典:Use a comma-separated list of在大括号内使用逗号分隔的key: value
pairs within braces:key: value
对列表:{'jack': 4098, 'sjoerd': 4127}
or{4098: 'jack', 4127: 'sjoerd'}
Use a dict comprehension:使用dict推导:{}
,、{x: x ** 2 for x in range(10)}
Use the type constructor:使用类型构造函数:dict()
,、dict([('foo', 100), ('bar', 200)])
,、dict(foo=100, bar=200)
If no positional argument is given, an empty dictionary is created.如果没有给出位置参数,则会创建一个空字典。If a positional argument is given and it is a mapping object, a dictionary is created with the same key-value pairs as the mapping object.如果给定了一个位置参数,并且它是一个映射对象,则将创建一个字典,其中包含与映射对象相同的键值对。Otherwise, the positional argument must be an iterable object.否则,位置参数必须是iterable对象。Each item in the iterable must itself be an iterable with exactly two objects.iterable中的每一项本身都必须是具有两个对象的iterable。The first object of each item becomes a key in the new dictionary, and the second object the corresponding value.每个项的第一个对象成为新字典中的键,第二个对象成为相应的值。If a key occurs more than once, the last value for that key becomes the corresponding value in the new dictionary.如果某个键出现多次,则该键的最后一个值将成为新字典中的相应值。If keyword arguments are given, the keyword arguments and their values are added to the dictionary created from the positional argument.如果给定了关键字参数,则关键字参数及其值将添加到从位置参数创建的词典中。If a key being added is already present, the value from the keyword argument replaces the value from the positional argument.如果已存在要添加的键,则关键字参数中的值将替换位置参数中的值。To illustrate, the following examples all return a dictionary equal to为了举例说明,以下示例都返回一个等于{"one": 1, "two": 2, "three": 3}
:{"one": 1, "two": 2, "three": 3}
的字典:>>> a = dict(one=1, two=2, three=3)
>>> b = {'one': 1, 'two': 2, 'three': 3}
>>> c = dict(zip(['one', 'two', 'three'], [1, 2, 3]))
>>> d = dict([('two', 2), ('one', 1), ('three', 3)])
>>> e = dict({'three': 3, 'one': 1, 'two': 2})
>>> f = dict({'one': 1, 'three': 3}, two=2)
>>> a == b == c == d == e == f
TrueProviding keyword arguments as in the first example only works for keys that are valid Python identifiers.在第一个示例中提供关键字参数仅适用于有效Python标识符的键。Otherwise, any valid keys can be used.否则,可以使用任何有效密钥。These are the operations that dictionaries support (and therefore, custom mapping types should support too):这些是字典支持的操作(因此,自定义映射类型也应该支持):-
list(d)
Return a list of all the keys used in the dictionary d.返回字典d中使用的所有键的列表。
-
len(d)
Return the number of items in the dictionary d.返回字典d中的项数。
-
d[key]
Return the item of d with key key.返回带有键key的d项。Raises a如果key不在映射中,则引发KeyError
if key is not in the map.KeyError
。If a subclass of dict defines a method如果dict的子类定义了一个方法__missing__()
and key is not present, thed[key]
operation calls that method with the key key as argument.__missing__()
,并且key不存在,则d[key]
操作将使用键key作为参数调用该方法。The然后,d[key]
operation then returns or raises whatever is returned or raised by the__missing__(key)
call.d[key]
操作返回或引发由__missing__(key)
调用返回或引发的任何内容。No other operations or methods invoke没有其他操作或方法调用__missing__()
.__missing__()
。If如果未定义__missing__()
is not defined,KeyError
is raised.__missing__()
,则会引发KeyError
。__missing__()
must be a method; it cannot be an instance variable:必须是一种方法;它不能是实例变量:>>> class Counter(dict):
... def __missing__(self, key):
... return 0
>>> c = Counter()
>>> c['red']
0
>>> c['red'] += 1
>>> c['red']
1The example above shows part of the implementation of上面的示例显示了collections.Counter
.collections.Counter
的部分实现。A different__missing__
method is used bycollections.defaultdict
.collections.defaultdict
使用了不同的__missing__
方法。
-
d[key] = value
Set将d[key]
to value.d[key]
设置为value。
-
del d[key]
Remove从d中删除d[key]
from d.d[key]
。Raises a如果key不在映射中,则引发KeyError
if key is not in the map.KeyError
。
-
key in d
Return如果d有键key,则返回True
if d has a key key, elseFalse
.True
,否则返回False
。
-
key not in d
Equivalent to相当于not key in d
.not key in d
。
-
iter(d)
Return an iterator over the keys of the dictionary.返回字典键的迭代器。This is a shortcut for这是iter(d.keys())
.iter(d.keys())
的快捷方式。
-
clear
()¶ Remove all items from the dictionary.从字典中删除所有项目。
-
copy
()¶ Return a shallow copy of the dictionary.返回字典的浅显副本。
-
classmethod
fromkeys
(iterable[, value])¶ Create a new dictionary with keys from iterable and values set to value.使用iterable中的键和设置为value的值创建一个新字典。fromkeys()
is a class method that returns a new dictionary.是返回新词典的类方法。valuedefaults to默认为None
.None
。All of the values refer to just a single instance, so it generally doesn’t make sense for value to be a mutable object such as an empty list.所有的值都只引用一个实例,所以将value设置为可变对象(如空列表)通常没有意义。To get distinct values, use a dict comprehension instead.要获得不同的值,请使用dict
推导。
-
get
(key[, default])¶ Return the value for key if key is in the dictionary, else default.如果key在字典中,则返回key的值,否则返回default。If default is not given, it defaults to如果未给定default,则默认为None
, so that this method never raises aKeyError
.None
,这样该方法就不会引发KeyError
。
-
items
()¶ Return a new view of the dictionary’s items (返回字典项((key, value)
pairs).(key, value)
)对的新视图。See the documentation of view objects.请参见视图对象的文档。
-
keys
()¶ Return a new view of the dictionary’s keys.返回字典键的新视图。See the documentation of view objects.请参见视图对象的文档。
-
pop
(key[, default])¶ If key is in the dictionary, remove it and return its value, else return default.如果key在字典中,请删除它并返回其值,否则返回default。If default is not given and key is not in the dictionary, a如果未给定default,并且key不在字典中,则会引发KeyError
is raised.KeyError
。
-
popitem
()¶ Remove and return a从字典中删除并返回(key, value)
pair from the dictionary.(key, value)
对。Pairs are returned in LIFO order.对按后进先出顺序返回。popitem()
is useful to destructively iterate over a dictionary, as often used in set algorithms.对于在字典上进行破坏性迭代很有用,这在集合算法中经常使用。If the dictionary is empty, calling如果字典为空,则调用popitem()
raises aKeyError
.popitem()
会引发KeyError
。
-
reversed(d)
Return a reverse iterator over the keys of the dictionary.返回字典键上的反向迭代器。This is a shortcut for这是reversed(d.keys())
.reversed(d.keys())
的快捷方式。New in version 3.8.版本3.8中新增。
-
setdefault
(key[, default])¶ If key is in the dictionary, return its value.如果key在字典中,则返回其值。If not, insert key with a value of default and return default.如果不是,请插入值为default的key并返回default。defaultdefaults to默认为None
.None
。
-
update
([other])¶ Update the dictionary with the key/value pairs from other, overwriting existing keys.使用other键/值对更新字典,覆盖现有键。Return返回None
.None
。update()
accepts either another dictionary object or an iterable of key/value pairs (as tuples or other iterables of length two).接受另一个dictionary对象或键/值对的iterable(作为元组或其他长度为2的iterable)。If keyword arguments are specified, the dictionary is then updated with those key/value pairs:如果指定了关键字参数,则使用这些键/值对更新字典:d.update(red=1, blue=2)
.d.update(red=1, blue=2)
。
-
values
()¶ Return a new view of the dictionary’s values.返回字典值的新视图。See the documentation of view objects.请参见视图对象的文档。An equality comparison between one一个dict.values()
view and another will always returnFalse
.dict.values()
视图与另一个视图之间的相等比较将始终返回False
。This also applies when comparing将dict.values()
to itself:dict.values()
与自身进行比较时,这也适用:>>> d = {'a': 1}
>>> d.values() == d.values()
False
-
d | other
Create a new dictionary with the merged keys and values of d and other, which must both be dictionaries.使用合并键和值d和other创建一个新字典,这两者都必须是字典。The values of other take priority when d and other share keys.当d和other共享键时,other值优先。New in version 3.9.版本3.9中新增。
-
d |= other
Update the dictionary d with keys and values from other, which may be either a mapping or an iterable of key/value pairs.使用来自other的键和值更新字典d,这些键和值可以是键/值对的mapping或iterable。The values of other take priority when d and other share keys.当d和other共享密钥时,other的值优先。New in version 3.9.版本3.9中新增。
Dictionaries compare equal if and only if they have the same当且仅当字典具有相同的(key, value)
pairs (regardless of ordering).(key, value)
对(无论顺序如何)时,字典比较相等。Order comparisons (‘<’, ‘<=’, ‘>=’, ‘>’) raise顺序比较(“<”、“<=”、“>=”、“>”)引发TypeError
.TypeError
。Dictionaries preserve insertion order.词典保留插入顺序。Note that updating a key does not affect the order.请注意,更新密钥不会影响顺序。Keys added after deletion are inserted at the end.删除后添加的键将插入末尾。>>> d = {"one": 1, "two": 2, "three": 3, "four": 4}
>>> d
{'one': 1, 'two': 2, 'three': 3, 'four': 4}
>>> list(d)
['one', 'two', 'three', 'four']
>>> list(d.values())
[1, 2, 3, 4]
>>> d["one"] = 42
>>> d
{'one': 42, 'two': 2, 'three': 3, 'four': 4}
>>> del d["two"]
>>> d["two"] = None
>>> d
{'one': 42, 'three': 3, 'four': 4, 'two': None}Changed in version 3.7:版本3.7中更改:Dictionary order is guaranteed to be insertion order.字典顺序保证为插入顺序。This behavior was an implementation detail of CPython from 3.6.此行为是3.6中CPython的一个实现细节。Dictionaries and dictionary views are reversible.字典和字典视图是可逆的。>>> d = {"one": 1, "two": 2, "three": 3, "four": 4}
>>> d
{'one': 1, 'two': 2, 'three': 3, 'four': 4}
>>> list(reversed(d))
['four', 'three', 'two', 'one']
>>> list(reversed(d.values()))
[4, 3, 2, 1]
>>> list(reversed(d.items()))
[('four', 4), ('three', 3), ('two', 2), ('one', 1)]Changed in version 3.8:版本3.8中更改:Dictionaries are now reversible.字典现在是可逆的。
See also
types.MappingProxyType
can be used to create a read-only view of a 可用于创建dict
.dict
的只读视图。
Dictionary view objects字典视图对象¶
The objects returned by dict.keys()
, dict.values()
and dict.items()
are view objects. dict.keys()
、dict.values()
和dict.items()
返回的对象是视图对象。They provide a dynamic view on the dictionary’s entries, which means that when the dictionary changes, the view reflects these changes.它们提供了字典条目的动态视图,这意味着当字典更改时,视图会反映这些更改。
Dictionary views can be iterated over to yield their respective data, and support membership tests:可以迭代字典视图以生成各自的数据,并支持成员资格测试:
-
len(dictview)
Return the number of entries in the dictionary.返回字典中的条目数。
-
iter(dictview)
Return an iterator over the keys, values or items (represented as tuples of在字典中的键、值或项(表示为(key, value)
) in the dictionary.(key, value)
的元组)上返回迭代器。Keys and values are iterated over in insertion order.键和值按插入顺序迭代。This allows the creation of这允许使用(value, key)
pairs usingzip()
:pairs = zip(d.values(), d.keys())
.zip()
创建(value, key)
对:pairs = zip(d.values(), d.keys())
。Another way to create the same list is创建相同列表的另一种方法是pairs = [(v, k) for (k, v) in d.items()]
.pairs = [(v, k) for (k, v) in d.items()]
。Iterating views while adding or deleting entries in the dictionary may raise a在字典中添加或删除条目时迭代视图可能会引发RuntimeError
or fail to iterate over all entries.RuntimeError
或无法迭代所有条目。Changed in version 3.7:版本3.7中更改:Dictionary order is guaranteed to be insertion order.字典顺序保证为插入顺序。
-
x in dictview
Return如果x在基础字典的键、值或项中(在后一种情况下,x应该是True
if x is in the underlying dictionary’s keys, values or items (in the latter case, x should be a(key, value)
tuple).(key, value)
元组),则返回True
。
-
reversed(dictview)
Return a reverse iterator over the keys, values or items of the dictionary.返回字典中键、值或项的反向迭代器。The view will be iterated in reverse order of the insertion.视图将按与插入相反的顺序迭代。Changed in version 3.8:版本3.8中更改:Dictionary views are now reversible.字典视图现在是可逆的。
-
dictview.mapping
Return a返回一个types.MappingProxyType
that wraps the original dictionary to which the view refers.types.MappingProxyType
类型,该类型包装视图引用的原始词典。New in version 3.10.版本3.10中新增。
Keys views are set-like since their entries are unique and hashable. 键视图的设置类似于,因为它们的条目是唯一且可哈希的。If all values are hashable, so that 如果所有值都是可散列的,因此(key, value)
pairs are unique and hashable, then the items view is also set-like. (key, value)
对是唯一且可散列的,则项目视图也类似于集的。(Values views are not treated as set-like since the entries are generally not unique.) (由于条目通常不唯一,因此值视图不被视为类似集。)For set-like views, all of the operations defined for the abstract base class 对于类似集的视图,为抽象基类collections.abc.Set
are available (for example, ==
, <
, or ^
).collections.abc.Set
定义的所有操作都可用(例如,==
、<
或^
)。
An example of dictionary view usage:字典视图用法示例:
>>> dishes = {'eggs': 2, 'sausage': 1, 'bacon': 1, 'spam': 500}
>>> keys = dishes.keys()
>>> values = dishes.values()
>>> # iteration
>>> n = 0
>>> for val in values:
... n += val
>>> print(n)
504
>>> # keys and values are iterated over in the same order (insertion order)
>>> list(keys)
['eggs', 'sausage', 'bacon', 'spam']
>>> list(values)
[2, 1, 1, 500]
>>> # view objects are dynamic and reflect dict changes
>>> del dishes['eggs']
>>> del dishes['sausage']
>>> list(keys)
['bacon', 'spam']
>>> # set operations
>>> keys & {'eggs', 'bacon', 'salad'}
{'bacon'}
>>> keys ^ {'sausage', 'juice'}
{'juice', 'sausage', 'bacon', 'spam'}
>>> # get back a read-only proxy for the original dictionary
>>> values.mapping
mappingproxy({'eggs': 2, 'sausage': 1, 'bacon': 1, 'spam': 500})
>>> values.mapping['spam']
500
Context Manager Types上下文管理器类型¶
Python’s Python的with
statement supports the concept of a runtime context defined by a context manager. with
语句支持上下文管理器定义的运行时上下文的概念。This is implemented using a pair of methods that allow user-defined classes to define a runtime context that is entered before the statement body is executed and exited when the statement ends:这是使用一对方法实现的,这些方法允许用户定义的类定义在执行语句体之前输入的运行时上下文,并在语句结束时退出:
-
contextmanager.
__enter__
()¶ Enter the runtime context and return either this object or another object related to the runtime context.输入运行时上下文并返回此对象或与运行时上下文相关的其他对象。The value returned by this method is bound to the identifier in the此方法返回的值使用此上下文管理器绑定到as
clause ofwith
statements using this context manager.with
语句的as
子句中的标识符。An example of a context manager that returns itself is a file object.返回自身的上下文管理器的一个示例是文件对象。File objects return themselves from文件对象从__enter__()
to allowopen()
to be used as the context expression in awith
statement.__enter__()
返回自身,以允许open()
用作with
语句中的上下文表达式。An example of a context manager that returns a related object is the one returned by返回相关对象的上下文管理器的一个示例是decimal.localcontext()
.decimal.localcontext()
返回的对象。These managers set the active decimal context to a copy of the original decimal context and then return the copy.这些管理器将活动的十进制上下文设置为原始十进制上下文的副本,然后返回副本。This allows changes to be made to the current decimal context in the body of the这允许对with
statement without affecting code outside thewith
statement.with
语句主体中的当前十进制上下文进行更改,而不会影响with
语句之外的代码。
-
contextmanager.
__exit__
(exc_type, exc_val, exc_tb)¶ Exit the runtime context and return a Boolean flag indicating if any exception that occurred should be suppressed.退出运行时上下文并返回一个布尔标志,指示是否应抑制发生的任何异常。If an exception occurred while executing the body of the如果在执行with
statement, the arguments contain the exception type, value and traceback information.with
语句主体时发生异常,则参数包含异常类型、值和回溯信息。Otherwise, all three arguments are否则,所有三个参数均为None
.None
。Returning a true value from this method will cause the从该方法返回真值将导致with
statement to suppress the exception and continue execution with the statement immediately following thewith
statement.with
语句抑制异常,并在with语句之后立即使用该语句继续执行。Otherwise the exception continues propagating after this method has finished executing.否则,此方法执行完毕后,异常将继续传播。Exceptions that occur during execution of this method will replace any exception that occurred in the body of the执行此方法期间发生的异常将替换with
statement.with
语句主体中发生的任何异常。The exception passed in should never be reraised explicitly - instead, this method should return a false value to indicate that the method completed successfully and does not want to suppress the raised exception.绝不应显式重新引发传入的异常-相反,此方法应返回false
值,以指示该方法已成功完成,并且不希望抑制引发的异常。This allows context management code to easily detect whether or not an这允许上下文管理代码轻松检测__exit__()
method has actually failed.__exit__()
方法是否实际失败。
Python defines several context managers to support easy thread synchronisation, prompt closure of files or other objects, and simpler manipulation of the active decimal arithmetic context. Python定义了几个上下文管理器,以支持轻松的线程同步、文件或其他对象的快速关闭,以及对活动十进制算术上下文的更简单操作。The specific types are not treated specially beyond their implementation of the context management protocol. 除了上下文管理协议的实现之外,不会对特定类型进行特殊处理。See the 有关一些示例,请参阅contextlib
module for some examples.contextlib
模块。
Python’s generators and the Python的生成器和contextlib.contextmanager
decorator provide a convenient way to implement these protocols. contextlib.contextmanager
装饰器为实现这些协议提供了一种方便的方法。If a generator function is decorated with the 如果使用contextlib.contextmanager
decorator, it will return a context manager implementing the necessary __enter__()
and __exit__()
methods, rather than the iterator produced by an undecorated generator function.contextlib.contextmanager
装饰器装饰生成器函数,它将返回一个实现必要的__enter__()
和__exit__()
方法的上下文管理器,而不是由未装饰的生成器函数生成的迭代器。
Note that there is no specific slot for any of these methods in the type structure for Python objects in the Python/C API. 请注意,在Python/C API中,Python对象的类型结构中没有任何这些方法的特定槽。Extension types wanting to define these methods must provide them as a normal Python accessible method. 想要定义这些方法的扩展类型必须将它们作为普通的Python可访问方法提供。Compared to the overhead of setting up the runtime context, the overhead of a single class dictionary lookup is negligible.与设置运行时上下文的开销相比,单类字典查找的开销可以忽略不计。
Type Annotation Types — Generic Alias, Union类型注释类型:泛型别名、联合¶
The core built-in types for type annotations are Generic Alias and Union.类型注释的核心内置类型是泛型别名和联合。
Generic Alias Type泛型别名类型¶
GenericAlias
objects are generally created by subscripting a class. 对象通常是通过订阅类来创建的。They are most often used with container classes, such as 它们最常用于容器类,如list
or dict
. list
或dict
。For example, 例如,list[int]
is a GenericAlias
object created by subscripting the list
class with the argument int
. list[int]
是一个GenericAlias
对象,它是通过使用参数int
订阅list
类创建的。GenericAlias
objects are intended primarily for use with type annotations.对象主要用于类型注释。
Note
It is generally only possible to subscript a class if the class implements the special method 通常,只有当类实现了特殊方法__class_getitem__()
.__class_getitem__()
,才可能为类下标。
A GenericAlias
object acts as a proxy for a generic type, implementing parameterized generics.GenericAlias
对象充当泛型类型的代理,实现参数化泛型。
For a container class, the argument(s) supplied to a subscription of the class may indicate the type(s) of the elements an object contains. 对于容器类,提供给该类订阅的参数可能指示对象包含的元素的类型。For example, 例如,set[bytes]
can be used in type annotations to signify a set
in which all the elements are of type bytes
.set[bytes]
可以在类型注释中用于表示所有元素都是bytes
类型的set
。
For a class which defines 对于定义了__class_getitem__()
but is not a container, the argument(s) supplied to a subscription of the class will often indicate the return type(s) of one or more methods defined on an object. __class_getitem__()
但不是容器的类,提供给该类订阅的参数通常会指示在对象上定义的一个或多个方法的返回类型。For example, 例如,正则表达式可用于regular expressions
can be used on both the str
data type and the bytes
data type:str
数据类型和bytes
数据类型:
If如果x = re.search('foo', 'foo')
,x
will be a re.Match object where the return values ofx.group(0)
andx[0]
will both be of typestr
.x = re.search('foo', 'foo')
,则x
将是一个re.Match对象,其中x.group(0)
和x[0]
的返回值都将是str
类型。We can represent this kind of object in type annotations with the我们可以用GenericAlias
re.Match[str]
.GenericAlias
re.Match[str]
在类型注释中表示这种对象。If如果y = re.search(b'bar', b'bar')
, (note theb
forbytes
),y
will also be an instance ofre.Match
, but the return values ofy.group(0)
andy[0]
will both be of typebytes
.y = re.search(b'bar', b'bar')
,(注意b
代表bytes
),y
也将是re.Match
的实例,但y.group(0)
和y[0]
的返回值都将是bytes
类型。In type annotations, we would represent this variety of re.Match objects with在类型注释中,我们将用re.Match[bytes]
.re.Match[bytes]
来表示这种类型的re.Match
对象。
GenericAlias
objects are instances of the class 对象是types.GenericAlias
, which can also be used to create GenericAlias
objects directly.types.GenericAlias
类的实例,也可以用来直接创建GenericAlias
对象。
-
T[X, Y, ...]
Creates a创建表示类型GenericAlias
representing a typeT
parameterized by types X, Y, and more depending on theT
used.T
的GenericAlias
,该类型由类型X、Y和更多参数化,具体取决于所使用的T
。For example, a function expecting a例如,函数需要包含list
containingfloat
elements:float
元素的list
:def average(values: list[float]) -> float:
return sum(values) / len(values)Another example for mapping objects, using a使用dict
, which is a generic type expecting two type parameters representing the key type and the value type.dict
,mapping对象的另一个示例,dict
是一种泛型类型,需要两个类型参数来表示键类型和值类型。In this example, the function expects a在本例中,函数需要一个dict
with keys of typestr
and values of typeint
:dict
,其键类型为str
,值类型为int
:def send_post_request(url: str, body: dict[str, int]) -> None:
...
The builtin functions 内置函数isinstance()
and issubclass()
do not accept GenericAlias
types for their second argument:isinstance()
和issubclass()
不接受GenericAlias
类型作为其第二个参数:
>>> isinstance([1, 2], list[str])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: isinstance() argument 2 cannot be a parameterized generic
The Python runtime does not enforce type annotations. Python运行时不强制执行类型注释。This extends to generic types and their type parameters. 这将扩展到泛型类型及其类型参数。When creating a container object from a 从GenericAlias
, the elements in the container are not checked against their type. GenericAlias
创建容器对象时,不会根据其类型检查容器中的元素。For example, the following code is discouraged, but will run without errors:例如,不鼓励使用以下代码,但运行时不会出现错误:
>>> t = list[str]
>>> t([1, 2, 3])
[1, 2, 3]
Furthermore, parameterized generics erase type parameters during object creation:此外,参数化泛型会在对象创建期间擦除类型参数:
>>> t = list[str]
>>> type(t)
<class 'types.GenericAlias'>
>>> l = t()
>>> type(l)
<class 'list'>
Calling 在泛型上调用repr()
or str()
on a generic shows the parameterized type:repr()
或str()
将显示参数化类型:
>>> repr(list[int])
'list[int]'
>>> str(list[int])
'list[int]'
The 泛型容器的__getitem__()
method of generic containers will raise an exception to disallow mistakes like dict[str][str]
:__getitem__()
方法将引发一个异常,以禁止诸如dict[str][str]
之类的错误:
>>> dict[str][str]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: There are no type variables left in dict[str]
However, such expressions are valid when type variables are used. 然而,当使用类型变量时,这样的表达式是有效的。The index must have as many elements as there are type variable items in the 索引的元素数量必须与GenericAlias
object’s __args__
.GenericAlias
对象的__args__
中的类型变量项数量相同。
>>> from typing import TypeVar
>>> Y = TypeVar('Y')
>>> dict[str, Y][int]
dict[str, int]
Standard Generic Classes标准泛型类¶
The following standard library classes support parameterized generics. 以下标准库类支持参数化泛型。This list is non-exhaustive.此列表并非详尽无遗。
Special Attributes of GenericAlias
objectsGenericAlias
对象的特殊属性¶
GenericAlias
objectsAll parameterized generics implement special read-only attributes.所有参数化泛型都实现特殊的只读属性。
-
genericalias.
__origin__
¶ This attribute points at the non-parameterized generic class:此属性指向非参数化泛型类:>>> list[int].__origin__
<class 'list'>
-
genericalias.
__args__
¶ This attribute is a此属性是传递给泛型类的原始tuple
(possibly of length 1) of generic types passed to the original__class_getitem__()
of the generic class:__class_getitem__()
的泛型类型的tuple
(可能长度为1):>>> dict[str, list[int]].__args__
(<class 'str'>, list[int])
-
genericalias.
__parameters__
¶ This attribute is a lazily computed tuple (possibly empty) of unique type variables found in此属性是唯一类型变量的延迟计算元组(可能为空),这些变量位于__args__
:__args__
中:>>> from typing import TypeVar
>>> T = TypeVar('T')
>>> list[T].__parameters__
(~T,)Note
A具有GenericAlias
object withtyping.ParamSpec
parameters may not have correct__parameters__
after substitution becausetyping.ParamSpec
is intended primarily for static type checking.typing.ParamSpec
参数的GenericAlias
对象在替换后可能没有正确的__parameters__
,因为typing.ParamSpec
主要用于静态类型检查。
See also另请参见
- PEP 484 -
Type Hints键入提示 Introducing Python’s framework for type annotations.介绍Python的类型注释框架。- PEP 585 -
Type Hinting Generics In Standard Collections标准集合中的类型暗示泛型 Introducing the ability to natively parameterize standard-library classes, provided they implement the special class method引入了本机参数化标准库类的能力,前提是它们实现了特殊的类方法__class_getitem__()
.__class_getitem__()
。- Generics, user-defined generics and
typing.Generic
Documentation on how to implement generic classes that can be parameterized at runtime and understood by static type-checkers.关于如何实现泛型类的文档,这些泛型类可以在运行时参数化,并且可以被静态类型检查器理解。
New in version 3.9.版本3.9中新增。
Union Type联合类型¶
A union object holds the value of the 联合对象保存多个类型对象上的|
(bitwise or) operation on multiple type objects. |
(按位或)操作的值。These types are intended primarily for type annotations. 这些类型主要用于类型注释。The union type expression enables cleaner type hinting syntax compared to 与typing.Union
.typing.Union
相比,联合类型表达式支持更简洁的类型暗示语法。
-
X | Y | ...
Defines a union object which holds types X, Y, and so forth.定义包含类型X、Y等的联合对象。X | Y
means either X or Y.表示X或Y。It is equivalent to它等价于typing.Union[X, Y]
.typing.Union[X, Y]
。For example, the following function expects an argument of type例如,以下函数需要int
orfloat
:int
或float
类型的参数:def square(number: int | float) -> int | float:
return number ** 2
-
union_object == other
Union objects can be tested for equality with other union objects.可以测试联合对象是否与其他联合对象相等。Details:详细信息:Unions of unions are flattened:并集的并集被展平:(int | str) | float == int | str | float
Redundant types are removed:删除冗余类型:int | str | int == int | str
When comparing unions, the order is ignored:比较联合时,忽略顺序:int | str == str | int
It is compatible with它与typing.Union
:typing.Union
兼容:int | str == typing.Union[int, str]
Optional types can be spelled as a union with可选类型可以拼写为None
:None
的联合:str | None == typing.Optional[str]
-
isinstance(obj, union_object)
-
issubclass(obj, union_object)
Calls tounion对象还支持对isinstance()
andissubclass()
are also supported with a union object:isinstance()
和issubclass()
的调用:>>> isinstance("", int | str)
TrueHowever, union objects containing parameterized generics cannot be used:但是,不能使用包含参数化泛型的联合对象:>>> isinstance(1, int | list[int])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: isinstance() argument 2 cannot contain a parameterized generic
The user-exposed type for the union object can be accessed from 可以从types.UnionType
and used for isinstance()
checks. types.UnionType
访问union对象的用户公开类型,并用于isinstance()
检查。An object cannot be instantiated from the type:无法从以下类型实例化对象:
>>> import types
>>> isinstance(int | str, types.UnionType)
True
>>> types.UnionType()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: cannot create 'types.UnionType' instances
Note
The 为支持语法__or__()
method for type objects was added to support the syntax X | Y
. X | Y
,添加了类型对象的__or__()
方法。If a metaclass implements 如果元类实现了__or__()
, the Union may override it:__or__()
,则联合可能会覆盖它:
>>> class M(type):
... def __or__(self, other):
... return "Hello"
...
>>> class C(metaclass=M):
... pass
...
>>> C | int
'Hello'
>>> int | C
int | __main__.C
See also
PEP 604 – PEP proposing the PEP提出了X | Y
syntax and the Union type.X | Y
语法和联合类型。
New in version 3.10.版本3.10中新增。
Other Built-in Types其他内置类型¶
The interpreter supports several other kinds of objects. 解释器支持其他几种类型的对象。Most of these support only one or two operations.其中大多数只支持一个或两个操作。
Modules模块¶
The only special operation on a module is attribute access: 模块上唯一的特殊操作是属性访问:m.name
, where m is a module and name accesses a name defined in m’s symbol table. m.name
,其中m是一个模块,name访问在m的符号表中定义的名称。Module attributes can be assigned to. 模块属性可以指定给。(Note that the (请注意,严格来说,import
statement is not, strictly speaking, an operation on a module object; import foo
does not require a module object named foo to exist, rather it requires an (external) definition for a module named foo somewhere.)import
语句不是对模块对象的操作;import foo
不需要名为foo的模块对象存在,而是需要在某处为名为foo的模块定义(外部)
A special attribute of every module is 每个模块的一个特殊属性是__dict__
. __dict__
。This is the dictionary containing the module’s symbol table. 这是包含模块符号表的字典。Modifying this dictionary will actually change the module’s symbol table, but direct assignment to the 修改此字典实际上会更改模块的符号表,但不可能直接分配到__dict__
attribute is not possible (you can write m.__dict__['a'] = 1
, which defines m.a
to be 1
, but you can’t write m.__dict__ = {}
). __dict__
属性(您可以写入m.__dict__['a'] = 1
,它将m.a
定义为1
,但您不能写入m.__dict__ = {}
)。Modifying 不建议直接修改__dict__
directly is not recommended.__dict__
。
Modules built into the interpreter are written like this: 解释器中内置的模块是这样编写的:<module 'sys' (built-in)>
. <module 'sys' (built-in)>
。If loaded from a file, they are written as 如果从文件加载,它们将被写为<module 'os' from '/usr/local/lib/pythonX.Y/os.pyc'>
.<module 'os' from '/usr/local/lib/pythonX.Y/os.pyc'>
。
Classes and Class Instances类和类实例¶
See Objects, values and types and Class definitions for these.请参见对象、值和类型以及类定义。
Functions函数¶
Function objects are created by function definitions. 函数对象由函数定义创建。The only operation on a function object is to call it: 对函数对象的唯一操作是调用它:func(argument-list)
.func(argument-list)
。
There are really two flavors of function objects: built-in functions and user-defined functions. 函数对象实际上有两种风格:内置函数和用户定义函数。Both support the same operation (to call the function), but the implementation is different, hence the different object types.两者都支持相同的操作(调用函数),但实现不同,因此对象类型不同。
See Function definitions for more information.有关详细信息,请参阅函数定义。
Methods方法¶
Methods are functions that are called using the attribute notation. 方法是使用属性表示法调用的函数。There are two flavors: built-in methods (such as 有两种风格:内置方法(如列表上的append()
on lists) and class instance methods. append()
)和类实例方法。Built-in methods are described with the types that support them.内置方法是用支持它们的类型描述的。
If you access a method (a function defined in a class namespace) through an instance, you get a special object: a bound method (also called instance method) object. 如果通过实例访问方法(在类命名空间中定义的函数),则会得到一个特殊的对象:绑定方法(也称为实例方法)对象。When called, it will add the 调用时,它会将self
argument to the argument list. self
参数添加到参数列表中。Bound methods have two special read-only attributes: 绑定方法有两个特殊的只读属性:m.__self__
is the object on which the method operates, and m.__func__
is the function implementing the method. m.__self__
是该方法操作的对象,m.__func__
是实现该方法的函数。Calling 调用m(arg-1, arg-2, ..., arg-n)
is completely equivalent to calling m.__func__(m.__self__, arg-1, arg-2, ..., arg-n)
.m(arg-1, arg-2, ..., arg-n)
完全等同于调用m.__func__(m.__self__, arg-1, arg-2, ..., arg-n)
。
Like function objects, bound method objects support getting arbitrary attributes. 与函数对象一样,绑定方法对象支持获取任意属性。However, since method attributes are actually stored on the underlying function object (但是,由于方法属性实际上存储在基础函数对象(meth.__func__
), setting method attributes on bound methods is disallowed. meth.__func__
)上,因此不允许在绑定方法上设置方法属性。Attempting to set an attribute on a method results in an 尝试在方法上设置属性会导致引发AttributeError
being raised. AttributeError
。In order to set a method attribute, you need to explicitly set it on the underlying function object:要设置方法属性,需要在基础函数对象上显式设置它:
>>> class C:
... def method(self):
... pass
...
>>> c = C()
>>> c.method.whoami = 'my name is method' # can't set on the method
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'method' object has no attribute 'whoami'
>>> c.method.__func__.whoami = 'my name is method'
>>> c.method.whoami
'my name is method'
See The standard type hierarchy for more information.有关详细信息,请参阅标准类型层次结构。
Code Objects代码对象¶
Code objects are used by the implementation to represent “pseudo-compiled” executable Python code such as a function body. 代码对象被实现用来表示“伪编译”的可执行Python代码,例如函数体。They differ from function objects because they don’t contain a reference to their global execution environment. 它们不同于函数对象,因为它们不包含对其全局执行环境的引用。Code objects are returned by the built-in 代码对象由内置的compile()
function and can be extracted from function objects through their __code__
attribute. compile()
函数返回,可以通过其__code__
属性从函数对象中提取。See also the 另请参见code
module.code
模块。
Accessing 访问__code__
raises an auditing event object.__getattr__
with arguments obj
and "__code__"
.__code__
会引发审核事件object.__getattr__
,参数为obj
和"__code__"
。
A code object can be executed or evaluated by passing it (instead of a source string) to the 可以通过将代码对象(而不是源字符串)传递给exec()
or eval()
built-in functions.exec()
或eval()
内置函数来执行或计算代码对象。
See The standard type hierarchy for more information.有关详细信息,请参阅标准类型层次结构。
Type Objects类型对象¶
Type objects represent the various object types. 类型对象表示各种对象类型。An object’s type is accessed by the built-in function 对象的类型由内置函数type()
. type()
访问。There are no special operations on types. 类型上没有特殊操作。The standard module 标准模块types
defines names for all standard built-in types.types
定义所有标准内置类型的名称。
Types are written like this: 类型是这样写的:<class 'int'>
.<class 'int'>
。
The Null ObjectNull对象¶
This object is returned by functions that don’t explicitly return a value. 此对象由不显式返回值的函数返回。It supports no special operations. 它不支持特殊操作。There is exactly one null object, named 只有一个空对象,名为None
(a built-in name). None
(内置名称)。type(None)()
produces the same singleton.生成相同的单例。
It is written as 它写为None
.None
。
The Ellipsis ObjectEllipsis对象¶
This object is commonly used by slicing (see Slicings). 此对象通常用于切片(请参见切片)。It supports no special operations. 它不支持特殊操作。There is exactly one ellipsis object, named 只有一个省略号对象,名为Ellipsis
(a built-in name). Ellipsis
(内置名称)。type(Ellipsis)()
produces the 生成Ellipsis
singleton.Ellipsis
单例。
It is written as 它写为Ellipsis
or ...
.Ellipsis
或...
。
The NotImplemented ObjectNotImplemented对象¶
This object is returned from comparisons and binary operations when they are asked to operate on types they don’t support. 当要求比较和二进制操作对不支持的类型进行操作时,将从比较和二进制操作中返回此对象。See Comparisons for more information. 有关更多信息,请参阅比较。There is exactly one 只有一个NotImplemented
object. NotImplemented
对象。type(NotImplemented)()
produces the singleton instance.生成单例实例。
It is written as 它被写为NotImplemented
.NotImplemented
。
Boolean Values布尔值¶
Boolean values are the two constant objects 布尔值是两个常量对象False
and True
. False
和True
。They are used to represent truth values (although other values can also be considered false or true). 它们用于表示真值(尽管其他值也可以被视为假或真)。In numeric contexts (for example when used as the argument to an arithmetic operator), they behave like the integers 0 and 1, respectively. 在数字上下文中(例如,当用作算术运算符的参数时),它们的行为分别类似于整数0和1。The built-in function 如果可以将值解释为真值,则可以使用内置函数bool()
can be used to convert any value to a Boolean, if the value can be interpreted as a truth value (see section Truth Value Testing above).bool()
将任何值转换为布尔值(请参见上文的真值测试一节)。
They are written as 它们分别写为False
and True
, respectively.False
和True
。
Internal Objects内部对象¶
See The standard type hierarchy for this information. 有关此信息,请参阅标准类型层次结构。It describes stack frame objects, traceback objects, and slice objects.它描述堆栈帧对象、回溯对象和切片对象。
Special Attributes特殊属性¶
The implementation adds a few special read-only attributes to several object types, where they are relevant. 该实现将一些特殊的只读属性添加到几个相关的对象类型中。Some of these are not reported by the 其中一些没有由dir()
built-in function.dir()
内置函数报告。
-
object.
__dict__
¶ A dictionary or other mapping object used to store an object’s (writable) attributes.用于存储对象(可写)属性的字典或其他映射对象。
-
instance.
__class__
¶ The class to which a class instance belongs.类实例所属的类。
-
class.
__bases__
¶ The tuple of base classes of a class object.类对象基类的元组。
-
definition.
__name__
¶ The name of the class, function, method, descriptor, or generator instance.类、函数、方法、描述符或生成器实例的名称。
-
definition.
__qualname__
¶ The qualified name of the class, function, method, descriptor, or generator instance.类、函数、方法、描述符或生成器实例的限定名称。New in version 3.3.版本3.3中新增。
-
class.
__mro__
¶ This attribute is a tuple of classes that are considered when looking for base classes during method resolution.该属性是在方法解析期间查找基类时考虑的类的元组。
-
class.
mro
()¶ This method can be overridden by a metaclass to customize the method resolution order for its instances.元类可以重写此方法,以自定义其实例的方法解析顺序。It is called at class instantiation, and its result is stored in它在类实例化时调用,其结果存储在__mro__
.__mro__
中。
-
class.
__subclasses__
()¶ Each class keeps a list of weak references to its immediate subclasses.每个类都保留一个对其直接子类的弱引用列表。This method returns a list of all those references still alive.此方法返回所有仍处于活动状态的引用的列表。The list is in definition order.列表按定义顺序排列。Example:示例:>>> int.__subclasses__()
[<class 'bool'>]
Footnotes
- 1
Additional information on these special methods may be found in the Python Reference Manual (Basic customization).关于这些特殊方法的更多信息可以在Python参考手册(基本定制)中找到。- 2
As a consequence, the list因此,列表[1, 2]
is considered equal to[1.0, 2.0]
, and similarly for tuples.[1, 2]
被认为等于[1.0, 2.0]
,对于元组也是如此。- 3
They must have since the parser can’t tell the type of the operands.它们必须有,因为解析器无法判断操作数的类型。- 4(1,2,3,4)
Cased characters are those with general category property being one of “Lu” (Letter, uppercase), “Ll” (Letter, lowercase), or “Lt” (Letter, titlecase).大小写字符是那些一般类别属性为“Lu”(字母,大写)、“Ll”(字母,小写)或“Lt”(字母,标题)之一的字符。- 5(1,2)
To format only a tuple you should therefore provide a singleton tuple whose only element is the tuple to be formatted.因此,要仅格式化一个元组,您应该提供一个单元组,其唯一元素是要格式化的元组。