Python数据类型概述
浏览量:797
Python有一些标准类型用于定义操作上,Python有五个标准的数据类型:
1、Numbers(数字)
2、String(字符串)
3、List(列表)
4、Tuple(元组)
5、Dictionary(字典)
1、Numbers(数字)
The interpreter acts as a simple calculator: you can type an expression at it and it will write the value. Expression syntax is straightforward: the operators +, -, * and / work just like in most other languages (for example, Pascal or C); parentheses (()) can be used for grouping. For example:
>>> 2 + 2 4 >>> 50 - 5 * 6 20 >>> (50 - 5 * 6 ) / 4 5.0 >>> 8 / 5 #division总是返回一个浮点数 1.6
Python支持四种不同的数值类型:
int(有符号整型)
long(长整型[也可以代表八进制和十六进制])
float(浮点型)
complex(复数)
示例:一些数值类型举例
| int | long | float | complex |
|---|---|---|---|
| 10 | 51924361L | 0.0 | 3.14j |
| 100 | -0x19323L | 15.20 | 45.j |
| -786 | 0122L | -21.9 | 9.322e-36j |
| 080 | 0xDEFABCECBDAECBFBAEl | 32.3+e18 | .876j |
| -0490 | 535633629843L | -90. | -.6545+0J |
| -0x260 | -052318172735L | -32.54e100 | 3e+26J |
| 0x69 | -4721885298529L | 70.2-E12 | 4.53e-7j |
长整型也可以使用小写"L",但是还是建议您使用大写"L",避免与数字"1"混淆。Python使用"L"来显示长整型。
Python还支持复数,复数由实数部分和虚数部分构成,可以用a + bj,或者complex(a,b)表示, 复数的实部a和虚部b都是浮点型
2、String(字符串)
Besides numbers, Python can also manipulate strings, which can be expressed in several ways. They can be enclosed in single quotes ('...') or double quotes ("...") with the same result [2]. \ can be used to escape quotes:
>>> 'spam eggs' # single quotes 'spam eggs' >>> 'doesn\'t' # use \' to escape the single quote... "doesn't" >>> "doesn't" # ...or use double quotes instead" doesn't" >>> '"Yes," he said.''"Yes," he said.' >>> "\"Yes,\" he said."'"Yes," he said.' >>> '"Isn\'t," she said.'' "Isn\'t," she said.'
In the interactive interpreter, the output string is enclosed in quotes and special characters are escaped with backslashes. While this might sometimes look different from the input (the enclosing quotes could change), the two strings are equivalent. The string is enclosed in double quotes if the string contains a single quote and no double quotes, otherwise it is enclosed in single quotes. The print() function produces a more readable output, by omitting the enclosing quotes and by printing escaped and special characters:
>>> '"Isn\'t," she said.'
'"Isn\'t," she said.'
>>> print('"Isn\'t," she said.')
"Isn't," she said.
>>> s = 'First line.\nSecond line.' # \n means newline
>>> s # without print(), \n is included in the output
'First line.\nSecond line.'
>>> print(s) # with print(), \n produces a new line
First line.
Second line. If you don’t want characters prefaced by \ to be interpreted as special characters, you can use raw strings by adding an rbefore the first quote:
>>> print('C:\some\name') # here \n means newline!
C:\some
ame
>>> print(r'C:\some\name') # note the r before the quote
C:\some\name String literals can span multiple lines. One way is using triple-quotes: """...""" or '''...'''. End of lines are automatically included in the string, but it’s possible to prevent this by adding a \ at the end of the line. The following example:
print("""\
Usage: thingy [OPTIONS]
-h Display this usage message
-H hostname Hostname to connect to
""")produces the following output (note that the initial newline is not included):
Usage: thingy [OPTIONS] -h Display this usage message -H hostname Hostname to connect to
Strings can be concatenated (glued together) with the + operator, and repeated with *:
>>> # 3 times 'un', followed by 'ium' >>> 3 * 'un' + 'ium' 'unununium'
Two or more string literals (i.e. the ones enclosed between quotes) next to each other are automatically concatenated.
>>> 'Py' 'thon' 'Python'
This only works with two literals though, not with variables or expressions:
>>> prefix = 'Py'
>>> prefix 'thon' # can't concatenate a variable and a string literal
...
SyntaxError: invalid syntax
>>> ('un' * 3) 'ium'
...
SyntaxError: invalid syntax If you want to concatenate variables or a variable and a literal, use +:
>>> prefix + 'thon' 'Python'
This feature is particularly useful when you want to break long strings:
>>> text = ('Put several strings within parentheses '
... 'to have them joined together.')
>>> text
'Put several strings within parentheses to have them joined together.'Strings can be indexed (subscripted), with the first character having index 0. There is no separate character type; a character is simply a string of size one:
它是编程语言中表示文本的数据类型。
python的字串列表有2种取值顺序:
从左到右索引默认0开始的,最大范围是字符串长度少1
从右到左索引默认-1开始的,最大范围是字符串开头
如果你的实要取得一段子串的话,可以用到变量[头下标:尾下标],就可以截取相应的字符串,其中下标是从0开始算起,可以是正数或负数,下标可以为空表示取到头或尾。
比如:
>>> word = 'Python' >>> word[0] # character in position 0 'P' >>> word[5] # character in position 5 'n'
当使用以冒号分隔的字符串,python返回一个新的对象,结果包含了以这对偏移标识的连续的内容,左边的开始是包含了下边界。
上面的结果包含了Word[1]的值y,而取到的最大范围不包括上边界,就是Word[5]的值n。
加号(+)是字符串连接运算符,星号(*)是重复操作。如下实例:
Indices may also be negative numbers, to start counting from the right:
>>> word[-1] # last character 'n' >>> word[-2] # second-last character 'o' >>> word[-6] 'P'
Note that since -0 is the same as 0, negative indices start from -1.
In addition to indexing, slicing is also supported. While indexing is used to obtain individual characters, slicing allows you to obtain substring:
>>> word[0:2] # characters from position 0 (included) to 2 (excluded) 'Py' >>> word[2:5] # characters from position 2 (included) to 5 (excluded) 'tho'
Note how the start is always included, and the end always excluded. This makes sure that s[:i] + s[i:] is always equal to s:
>>> word[:2] + word[2:] 'Python' >>> word[:4] + word[4:] 'Python'
Slice indices have useful defaults; an omitted first index defaults to zero, an omitted second index defaults to the size of the string being sliced.
>>> word[:2] # character from the beginning to position 2 (excluded) 'Py' >>> word[4:] # characters from position 4 (included) to the end 'on' >>> word[-2:] # characters from the second-last (included) to the end 'on'
One way to remember how slices work is to think of the indices as pointing between characters, with the left edge of the first character numbered 0. Then the right edge of the last character of a string of n characters has index n, for example:
+---+---+---+---+---+---+ | P | y | t | h | o | n | +---+---+---+---+---+---+ 0 1 2 3 4 5 -6 -5 -4 -3 -2 -1
The first row of numbers gives the position of the indices 0...6 in the string; the second row gives the corresponding negative indices. The slice from i to j consists of all characters between the edges labeled i and j, respectively.
For non-negative indices, the length of a slice is the difference of the indices, if both are within bounds. For example, the length of word[1:3] is 2.
Attempting to use an index that is too large will result in an error:
>>> word[42] # the word only has 6 characters Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: string index out of range
However, out of range slice indexes are handled gracefully when used for slicing:
>>> word[4:42] 'on' >>> word[42:] ''
Python strings cannot be changed — they are immutable. Therefore, assigning to an indexed position in the string results in an error:
>>> word[0] = 'J' ... TypeError: 'str' object does not support item assignment >>> word[2:] = 'py' ... TypeError: 'str' object does not support item assignment
If you need a different string, you should create a new one:
>>> 'J' + word[1:] 'Jython' >>> word[:2] + 'py' 'Pypy'
The built-in function len() returns the length of a string:
>>> s = 'supercalifragilisticexpialidocious' >>> len(s)34
3、List(列表)
Python knows a number of compound data types, used to group together other values. The most versatile is the list, which can be written as a list of comma-separated values (items) between square brackets. Lists might contain items of different types, but usually the items all have the same type.
Python知道一些复合数据类型,用于将其他值组合在一起。最通用的是列表,可以写成方括号之间的逗号分隔值(项)的列表。列表可能包含不同类型的项目,但通常这些项目都具有相同的类型。
>>> squares = [1, 4, 9, 16, 25] >>> squares [1, 4, 9, 16, 25]
Like strings (and all other built-in sequence type), lists can be indexed and sliced:.
像字符串(和所有其他内置的序列类型),列表可以索引和切片:
>>> squares[0] # indexing returns the item 1 >>> squares[-1] 25 >>> squares[-3:] # slicing returns a new list [9, 16, 25]
All slice operations return a new list containing the requested elements. This means that the following slice returns a new (shallow) copy of the list:
所有切片操作返回包含所请求元素的新列表。这意味着以下切片返回列表的新(浅)副本:
>>> squares[:] [1, 4, 9, 16, 25]
Lists also support operations like concatenation:
列表还支持类似串联的操作:
>>> squares + [36, 49, 64, 81, 100] [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
Unlike strings, which are immutable, lists are a mutable type, i.e. it is possible to change their content:
不同于不可变的字符串,列表是可变 类型,即可以更改其内容:
>>> cubes = [1, 8, 27, 65, 125] # something's wrong here >>> 4 ** 3 # the cube of 4 is 64, not 65! 64 >>> cubes[3] = 64 # replace the wrong value >>> cubes [1, 8, 27, 64, 125]
You can also add new items at the end of the list, by using the append() method (we will see more about methods later):
>>> cubes.append(216) # add the cube of 6 >>> cubes.append(7 ** 3) # and the cube of 7 >>> cubes [1, 8, 27, 64, 125, 216, 343]
Assignment to slices is also possible, and this can even change the size of the list or clear it entirely:
分配到切片也是可能的,这甚至可以更改列表的大小或完全清除它:
>>> letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] >>> letters ['a', 'b', 'c', 'd', 'e', 'f', 'g'] >>> # replace some values >>> letters[2:5] = ['C', 'D', 'E'] >>> letters ['a', 'b', 'C', 'D', 'E', 'f', 'g'] >>> # now remove them >>> letters[2:5] = [] >>> letters ['a', 'b', 'f', 'g'] >>> # clear the list by replacing all the elements with an empty list >>> letters[:] = [] >>> letters []
The built-in function len() also applies to lists:
>>> letters = ['a', 'b', 'c', 'd'] >>> len(letters) 4
It is possible to nest lists (create lists containing other lists), for example:
可以嵌套列表(创建包含其他列表的列表),例如:
>>> a = ['a', 'b', 'c'] >>> n = [1, 2, 3] >>> x = [a, n] >>> x [['a', 'b', 'c'], [1, 2, 3]] >>> x[0] ['a', 'b', 'c'] >>> x[0][1] 'b'
4.元组
We saw that lists and strings have many common properties, such as indexing and slicing operations. They are two examples of sequence data types (see Sequence Types — list, tuple, range). Since Python is an evolving language, other sequence data types may be added. There is also another standard sequence data type: the tuple.
A tuple consists of a number of values separated by commas, for instance:
我们看到列表和字符串有许多常见的属性,如索引和切片操作。它们是序列数据类型的两个示例(请参见 序列类型 - 列表,元组,范围)。由于Python是一种演进的语言,可以添加其他序列数据类型。还有另一种标准序列数据类型: 元组。
元组由许多用逗号分隔的值组成,例如:
>>> t = 12345, 54321, 'hello!' >>> t[0] 12345 >>> t (12345, 54321, 'hello!') >>> # Tuples may be nested: ... u = t, (1, 2, 3, 4, 5) >>> u ((12345, 54321, 'hello!'), (1, 2, 3, 4, 5)) >>> # Tuples are immutable: ... t[0] = 88888 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'tuple' object does not support item assignment >>> # but they can contain mutable objects: ... v = ([1, 2, 3], [3, 2, 1]) >>> v ([1, 2, 3], [3, 2, 1])
As you see, on output tuples are always enclosed in parentheses, so that nested tuples are interpreted correctly; they may be input with or without surrounding parentheses, although often parentheses are necessary anyway (if the tuple is part of a larger expression). It is not possible to assign to the individual items of a tuple, however it is possible to create tuples which contain mutable objects, such as lists.
如你所见,输出元组总是包含在括号中,以便嵌套元组被正确解释; 它们可以在有或没有环绕括号的情况下输入,尽管经常括号是必要的(如果元组是较大表达式的一部分)。不可能分配到元组的各个项目,但是可以创建包含可变对象(例如列表)的元组。
Though tuples may seem similar to lists, they are often used in different situations and for different purposes. Tuples are immutable, and usually contain a heterogeneous sequence of elements that are accessed via unpacking (see later in this section) or indexing (or even by attribute in the case of namedtuples). Lists are mutable, and their elements are usually homogeneous and are accessed by iterating over the list.
尽管元组可能看起来类似于列表,但它们通常用于不同的情况和不同的目的。元组是不可变的,并且通常包含通过解包(参见本节后面的内容)或索引(甚至在属性的情况下namedtuples)访问的异构元素序列。列表是可变的,并且它们的元素通常是同构的,并且通过在列表上迭代来访问。
A special problem is the construction of tuples containing 0 or 1 items: the syntax has some extra quirks to accommodate these. Empty tuples are constructed by an empty pair of parentheses; a tuple with one item is constructed by following a value with a comma (it is not sufficient to enclose a single value in parentheses). Ugly, but effective. For example:
一个特殊的问题是构造包含0或1个项目的元组:语法有一些额外的奇怪,以适应这些。空元组由一对空圆括号构成; 具有一个项的元组通过使用逗号跟随一个值来构造(在括号中包围单个值是不够的)。丑,但有效。例如:
>>> empty = ()
>>> singleton = 'hello', # <-- note trailing comma
>>> len(empty)
0
>>> len(singleton)
1
>>> singleton
('hello',) The statement t = 12345, 54321, 'hello!' is an example of tuple packing: the values 12345, 54321 and 'hello!' are packed together in a tuple. The reverse operation is also possible:+
该语句是元组包装的一个例子:值,并在一个元组中打包在一起。反向操作也是可的:t = 12345, 54321,'hello!'1234554321'hello!'
>>> x, y, z = t
This is called, appropriately enough, sequence unpacking and works for any sequence on the right-hand side. Sequence unpacking requires that there are as many variables on the left side of the equals sign as there are elements in the sequence. Note that multiple assignment is really just a combination of tuple packing and sequence unpacking.
这被称为,足够恰当序列解包,并适用于任何序列在右手边。序列拆包要求在等号的左侧有与该序列中的元素一样多的变量。注意,多重赋值实际上只是元组打包和序列解包的组合。
5.字典
Another useful data type built into Python is the dictionary (see Mapping Types — dict). Dictionaries are sometimes found in other languages as “associative memories” or “associative arrays”. Unlike sequences, which are indexed by a range of numbers, dictionaries are indexed by keys, which can be any immutable type; strings and numbers can always be keys. Tuples can be used as keys if they contain only strings, numbers, or tuples; if a tuple contains any mutable object either directly or indirectly, it cannot be used as a key. You can’t use lists as keys, since lists can be modified in place using index assignments, slice assignments, or methods like append() and extend().
Python中内置的另一个有用的数据类型是字典(请参阅 映射类型 - dict)。字典有时在其他语言中被称为“关联存储器”或“关联数组”。与通过数字范围索引的序列不同,字典由键索引,键可以是任何不可变类型; 字符串和数字可以始终是键。元组可以用作键,如果它们只包含字符串,数字或元组; 如果元组直接或间接包含任何可变对象,则不能将其用作键。您不能将列表用作键,因为列表可以使用索引分配,切片分配或像append()和 等方法进行修改extend()。
It is best to think of a dictionary as an unordered set of key: value pairs, with the requirement that the keys are unique (within one dictionary). A pair of braces creates an empty dictionary: {}. Placing a comma-separated list of key:value pairs within the braces adds initial key:value pairs to the dictionary; this is also the way dictionaries are written on output.
最好将字典视为无序的一组键:值对,要求键是唯一的(在一个字典内)。一对大括号创建一个空字典:{}。在大括号中放置逗号分隔的键:值对列表将初始键:值对添加到字典中; 这也是词典在输出上写的方式。
The main operations on a dictionary are storing a value with some key and extracting the value given the key. It is also possible to delete a key:value pair with del. If you store using a key that is already in use, the old value associated with that key is forgotten. It is an error to extract a value using a non-existent key.
字典中的主要操作是使用一些键存储值,并提取给定键的值。也可以删除一个键:值对del。如果使用已经使用的密钥进行存储,则会忘记与该密钥相关联的旧值。使用不存在的密钥提取值时出错。
Performing list(d.keys()) on a dictionary returns a list of all the keys used in the dictionary, in arbitrary order (if you want it sorted, just use sorted(d.keys()) instead). [2] To check whether a single key is in the dictionary, use the in keyword.
Here is a small example using a dictionary:
在字典list(d.keys())上执行,可以按任意顺序返回字典中使用的所有键的列表(如果要排序,请 sorted(d.keys())改用)。[2] 要检查字典中是否有单个键,请使用in关键字。
这是一个使用字典的小例子:
>>> tel = {'jack': 4098, 'sape': 4139}
>>> tel['guido'] = 4127
>>> tel
{'sape': 4139, 'guido': 4127, 'jack': 4098}
>>> tel['jack']
4098
>>> del tel['sape']
>>> tel['irv'] = 4127
>>> tel
{'guido': 4127, 'irv': 4127, 'jack': 4098}
>>> list(tel.keys())
['irv', 'guido', 'jack']
>>> sorted(tel.keys())
['guido', 'irv', 'jack']
>>> 'guido' in tel
True
>>> 'jack' not in tel
False The dict() constructor builds dictionaries directly from sequences of key-value pairs:
该dict()构造函数直接从键-值对序列构建字典:
>>> dict([('sape', 4139), ('guido', 4127), ('jack', 4098)])
{'sape': 4139, 'jack': 4098, 'guido': 4127}In addition, dict comprehensions can be used to create dictionaries from arbitrary key and value expressions:
此外,可以使用dict的理解来从任意键和值表达式创建字典:
>>> {x: x**2 for x in (2, 4, 6)}{2: 4, 4: 16, 6: 36}When the keys are simple strings, it is sometimes easier to specify pairs using keyword arguments:
当键是简单的字符串时,使用关键字参数来指定对,有时更容易:
>>> dict(sape=4139, guido=4127, jack=4098)
{'sape': 4139, 'jack': 4098, 'guido': 4127}Looping Techniques
When looping through dictionaries, the key and corresponding value can be retrieved at the same time using the items() method.
当循环通过字典时,可以使用该items()方法同时检索关键字和对应的值。
>>> knights = {'gallahad': 'the pure', 'robin': 'the brave'}
>>> for k,v in knights.items():
... print(k,v)
...
('gallahad', 'the pure')
('robin', 'the brave') When looping through a sequence, the position index and corresponding value can be retrieved at the same time using the enumerate() function.
当循环遍历序列时,可以使用该enumerate()函数同时检索位置索引和相应的值。
>>> for i, v in enumerate(['tic', 'tac', 'toe']): ... print(i,v) ... (0, 'tic') (1, 'tac') (2, 'toe')
To loop over two or more sequences at the same time, the entries can be paired with the zip() function.
要同时循环两个或多个序列,条目可以与该zip()功能配对。
>>> questions = ['name', 'quest', 'favorite color']
>>> answers = ['lancelot', 'the holy grail', 'blue']
>>> for q, a in zip(questions, answers):
... print('What is your {0}? It is {1}.'.format(q, a))
...
What is your name? It is lancelot.
What is your quest? It is the holy grail.
What is your favorite color? It is blue. To loop over a sequence in reverse, first specify the sequence in a forward direction and then call the reversed() function.
要反过来循环一个序列,首先按顺序指定序列,然后调用该reversed()函数。
>>> for i in reversed(range(1, 10, 2)): ... print(i) ... 9 7 5 3 1 >>> for i in (range(1, 10, 2)): ... print(i) ... 1 3 5 7 9
To loop over a sequence in sorted order, use the sorted() function which returns a new sorted list while leaving the source unaltered.
要以排序顺序循环序列,请使用sorted()返回新排序列表的函数,而不改变源。
>>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana'] >>> for i in basket: ... print(i) ... apple orange apple pear orange banana >>> for i in sorted(basket): ... print(i) ... apple apple banana orange orange pear >>> for i in set(basket): ... print(i) ... orange pear apple banana >>> >>> for i in sorted(set(basket)): ... print(i) ... apple banana orange pear
It is sometimes tempting to change a list while you are looping over it; however, it is often simpler and safer to create a new list instead.
在循环列表时有时候会很有挑战性; 然而,创建新列表通常更简单和安全。
>>> import math
>>> raw_data = [56.2, float('NaN'), 51.7, 55.3, 52.5, float('NaN'), 47.8]
>>> filtered_data = []
>>> for value in raw_data:
... if not math.isnan(value):
... filtered_data.append(value)
...
>>> filtered_data
[56.2, 51.7, 55.3, 52.5, 47.8]补充:集合
Python also includes a data type for sets. A set is an unordered collection with no duplicate elements. Basic uses include membership testing and eliminating duplicate entries. Set objects also support mathematical operations like union, intersection, difference, and symmetric difference.
Python还包括集合的数据类型。一组是无序的集合,没有重复的元素。基本用法包括成员资格测试和消除重复条目。设置对象还支持数学运算,如联合,交集,差异和对称差异。
Curly braces or the set() function can be used to create sets. Note: to create an empty set you have to use set(), not {}; the latter creates an empty dictionary, a data structure that we discuss in the next section.
花括号或set()函数可用于创建集合。注意:要创建一个空集,你必须使用set(),而不是{}; 后者创建一个空字典,我们在下一节讨论的数据结构。
Here is a brief demonstration:
这里是一个简要的演示:
>>> basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
>>> print(basket) # show that duplicates have been removed
{'orange', 'banana', 'pear', 'apple'}
>>> 'orange' in basket # fast membership testing
True
>>> 'crabgrass' in basket
False
>>> # Demonstrate set operations on unique letters from two words
...
>>> a = set('abracadabra')
>>> b = set('alacazam')
>>> a # unique letters in a
{'a', 'r', 'b', 'c', 'd'}
>>> a - b # letters in a but not in b
{'r', 'd', 'b'}
>>> a | b # letters in either a or b
{'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'}
>>> a & b # letters in both a and b
{'a', 'c'}
>>> a ^ b # letters in a or b but not both
{'r', 'd', 'b', 'm', 'z', 'l'}Similarly to list comprehensions, set comprehensions are also supported:
与列表推导类似,也支持set comprehensions:
>>> a = {x for x in 'abracadabra' if x not in 'abc'}
>>> a
{'r', 'd'}
神回复
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。