映射和集合类型

更新时间:2023-06-30 15:45:03 阅读: 评论:0

Mapping and Set Types
映射和集合类型
Chapter Topics
Mapping Type: Dictionaries欧阳询的书法作品
Operators
Built-in Functions
Built-in Methods
Dictionary Keys
Set Types
Operators
Built-in Functions
Built-in Methods
Related Modules
7
*本章主题
* 映射类型: 字典
  * 操作符
  * 内建函数
  * 内建方法
  * 字典的键
* 集合类型
  * 操作符
  * 内建函数
  * 内建方法
* 相关模块
第七章
in this chapter, we take a look at Python’s mapping and t types. As in earlier chapters, an introduction is followed by a discussion of the applicable operators and factory and built-in functions (BIFs) and methods. We then go into more specific usage of each data type.
本章中,我们来讨论Python语言中的映射类型和集合类型。和前面的章节一样,我们首先做一个介绍,然后在来讨论可用操作符,工厂函数、内建函数(BIF)和方法。然后我们再来看看每种数据类型的详细用法。
7.1 Mapping Type: Dictionaries
映射类型:字典
Dictionaries are the sole mapping type in Python. Mapping objects have a one-to-many correspondence between hashable values (keys) and the objects they reprent (values). They are similar to Perl hashes and can be generally considered as mutable hash tables. A dictionary object itlf is mutable and is yet another container type that can store any number of Python objects, including other container types. What makes dictionaries different from quence type containers like lists and tuples is the way the data are stored and accesd. Sequence types u numeric keys only (numbered quentially as indexed offts from the beginning of the quence). Mapping types may u most other object types as keys; strings are the most common. Unlike quence type keys, mapping keys are often, if not directly, associated with the data value that is stored. But becau we are no longer using “quentially ordered” keys with mapping types, we are left with an unordered collection of data.
字典是Python语言中唯一的映射类型。映射类型对象里哈希值(键) 和指向的对象(值)是一对多的关系。 它们与Perl中的哈希类型(译者注:又称关联数组)相似,通常被认为是可变的哈希表。一个字典对象是可变的,它是一个容器类型,能存储任意个数的Python对象,其中也包括其他容器类型。字典类型和序列类型容器类(列表、元组)的区别是存储和访问数据的方式不同。序列类型只用数字类型的键(从序列的开始起按数值顺序索引)。映射类型可以用其他对象类型做键;一般最常见的是用字符串
做键(keys)。和序列类型的键不同,映射类型的键(keys)直接,或间接地和存储的数据值相关联。但因为在映射类型中,我们不再用"序列化排序"的键(keys),所以映射类型中的数据是无序排列的。
As it turns out, this does not hinder our u becau mappi
ng types do not require a numeric value to index into a container to obtain the desired item. With a key, you are “mapped” directly to your value, hence the term “mapping type.” The reason why they are commonly referred to as hash tables is becau that is the exact type of object that dictionaries are. Dictionaries are one of Python’s most powerful data types.
显然,这并不影响我们使用映射类型,因为映射类型不要求用数字值做索引以从一个容器中获取对应的数据项。你可以用键(key)直接 "映射" 到值, 这就是为什么叫映射类型(“mapping type”)的原因。映射类型通常被称做哈希表的原因是字典对象就是哈希类型的。字典是Python中最强大的数据类型之一。
CORE NOTE: What are hash tables and how do they relate to dictionaries?
核心笔记:什么是哈希表?它们与字典的关系是什么?
Sequence types u quentially ordered numeric keys as index offts to store your data in an array format. The index number usually has nothing to do with the data value that is being stored. Th
ere should also be a way to store data bad on another associated value such as a string. We do this all the time in everyday living. You file people’s phone numbers in your address book bad on last name, you add events to your calendar or appointment book bad on date and time, etc. For each of the examples, an associated value to a data item was your key.
序列类型用有序的数字键做索引将数据以数组的形式存储。一般,索引值与所存储的数据毫无关系。还可以用另一种方式来存储数据:基于某种相关值,比如说一个字符串。我们在日常生活中一直这么做。你把人们的电话号码按照他们的姓记录在电话簿上,你按照时间在日历或约会簿上添加事件,等等。在这些例子中,你的键(key)就是和数据项相关的值。
Hash tables are a data structure that does exactly what we described. They store each piece of data, called a value, bad on an associated data item, called a key. Together, the are known as key-value pairs. The hash table algorithm takes your key, performs an operation on it, called a hash function, and bad on the result of the calculation, choos where in the data structure to store your value. Where any one particular value is stored depends on what its key is. Becau of this randomness, there is no ordering of the values in the hash table. You have an unordered collection of data.
哈希表是一种数据结构:它按照我们所要求的去工作。哈希表中存储的每一条数据,叫做一个值(value),是根据与它相关的一个被称作为键(key)的数据项进行存储的。键和值合在一起被称为“键-值 对”(key-value pairs)。  哈希表的算法是获取键,对键执行一个叫做哈希函数的操作,并根据计算的结果,选择在数据结构的某个地址中来存储你的
值。任何一个值存储的地址皆取决于它的键。正因为这种随意性,哈希表中的值是没有顺序的。你拥有的是一个无序的数据集。
The only kind of ordering you can obtain is by taking either a dictionary’s t of keys or values.The keys() or values() method returns lists, which are sortable. You can also call items() to get a list of keys and values as tuple pairs and sort that. Dictionaries themlves have no implicit ordering becau they are hashes.
你所能获得的有序集合只能是字典中的键的集合或者值的集合。方法Keys() 或 values() 返回一个列表,该列表是可排序的。 你还可以用 items()方法得到包含键、值对的元组的列表来排序。由于字典本身是哈希的,所以是无序的。
Hash tables generally provide good performance becau lookups occur fairly quickly once you have a key.
哈希表一般有很好的性能, 因为用键查询相当快。
 Python dictionaries are implemented as resizeable hash tables. If you are familiar with Perl, then we can say that dictionaries are similar to Perl’s associative arrays or hashes.
  Python的字典是作为可变的哈希表实现的。如果你熟悉Perl的话, 就可以发现字典与Perl中的"关系数组"或散列相似。
 We  will  now  take  a  clor  look  at  Python  dictionaries.  The  syntax  of  a dictionary  entry  is  key:value. Also,  dictionary  entries  are  enclod  in braces ( { } ).
  现在我们就来研究Python字典。一个字典条目的语法格式是 键:值。 而且,多条字典条目被包含在( { } ) 里。
How to Create and Assign Dictionaries
网络推广策略
如何创建字典和给字典赋值
Creating  dictionaries  simply  involves  assigning  a  dictionary  to  a  variable, regardless of whether the dictionary has elements or not:
创建字典只需要把字典赋值给一个变量,不管这个字典是否包含元素:
>>> dict1 = {}
>>> dict2 = {'name': 'earth', 'port': 80}
>>> dict1, dict2
({}, {'port': 80, 'name': 'earth'})
In Python versions 2.2 and newer, dictionaries may also be created using the factory  function  dict().  We  discuss  more  examples  later  when  we  take  a clor look at dict(), but here’s a sneak peek for now:
从Python 2.2版本起, 可以用工厂方法 dict() 来创建字典。 当我们详细讨论dict()的时候会看到更多的例子,现在来看一个小例子:
>>> fdict = dict((['x', 1], ['y', 2]))
>>> fdict
{'y': 2, 'x': 1}
In Python versions 2.3 and newer, dictionaries may also be created using a very convenient built-in method for creating a “default” dictionary who elements all have the same value (defaulting to None if not given), fromkeys():
从Python 2.3版本起, 可以用一个很方便的内建方法fromkeys() 来创建一个"默认"字典, 字典中元素具有相同的值 (如果没有给出, 默认为None):
>>> ddict = {}.fromkeys(('x', 'y'), -1)
>>> ddict
{'y': -1, 'x': -1}
>>>
>
>> edict = {}.fromkeys(('foo', 'bar'))
>>> edict
{'foo': None, 'bar': None}
How to Access Values in Dictionaries
如何访问字典中的值
To traver a dictionary (normally by key), you only need to cycle through its keys, like this:
溶解度四要素要想遍历一个字典(一般用键), 你只需要循环查看它的键, 像这样:
微信电脑版怎么发朋友圈
>>> dict2 = {'name': 'earth', 'port': 80}cad配置
>>>
吴桂显>>>> for key in dict2.keys():
... print 'key=%s, value=%s' % (key, dict2[key])
...
key=name, value=earth key=port, value=80
Beginning with Python 2.2, you no longer need to u the keys() method to extract a list of keys to loop over. Iterators were created to simplify accessing  of  quence-like  objects  such  as  dictionaries  and  files.  Using  just the dictionary name itlf will cau an iterator over that dictionary to be ud in a for loop:
从Python 2.2 开始, 你可以不必再用keys()方法获取供循环使用的键值列表了。 可以
用迭代器来轻松地访问类序列对象(quence-like objects),比如字典和文件。只需要用字典的名字就可以在 for 循环里遍历字典。
>>> dict2 = {'name': 'earth', 'port': 80}
>>>
>>>> for key in dict2:
... print 'key=%s, value=%s' % (key, dict2[key])
...
key=name, value=earth key=port, value=80
 To  access  individual  dictionary  elements,  you  u  the  familiar  square brackets along with the key to obtain its value:
  要得到字典中某个元素的值, 可以用你所熟悉的字典键加上中括号来得到:
>>> dict2['name']
'earth'
>>>
>>> print 'host %s is running on port %d' % \
... (dict2['name'], dict2['port'])
host earth is running on port 80
 Dictionary dict1 defined above is empty while dict2 has two data items. The keys in dict2 are 'name' and 'port', and their associated value items are 'earth' and 80, respectively. Access to the valu
e is through the key, as you can e from the explicit access to the 'name' key.
  字典dict1是空的,字典dict2有两个数据元素。字典dict2的键是 'name' 和 'port',它们对应的值分别是'earth' 和 80。就像你看到的,通过键'name'可以得到字典中的元素的值。
 If we attempt to access a data item with a key that is not part of the dictionary, we get an error:
  如果我们想访问该字典中的一个数据元素,而它在这个字典中没有对应的键,将会产生一个错误:
>>> dict2['rver'] Traceback (innermost last):
 File "<stdin>", line 1, in ? KeyError: rver
 In this example, we tried to access a value with the key 'rver' which, as you know from the code above, does not exist. The best way to check if a dictionary has a specific key is to u the dictionary’s  has_key() method, or better yet,  the  in or  not in operators  starting  with  version  2.2.  The has_key() method will be obsoleted in future versions of Python, so it is best to just u in or not in.
  在这个例子中,我们试图获得字典中'rver'键所对应的值。你从上面的代
码知道,'rver'这个键并不存在。检查一个字典中是否有某个键的最好方法是用字典的 has_key()方法, 或者另一种比较好的方法就是从2.2版本起用的,in 或  not in 操作符。 has_key() 方法将会在未来的Python版本中弃用,所以用in 或  not in是最好的方法。
We  will  introduce  all  of  a  dictionary’s  methods  below.  The  Boolean has_key() and the in and not in operators are Boolean, returning True if a dictionary has that key and Fal otherwi. (In Python versions preceding Boolean constants [older than 2.3], the values returned are 1 and 0, respectively.)
下面我们将介绍字典所有的方法。方法has_key()和 in 以及 not in 操作符都是布尔类型的。对于前两者而言,如果字典中有该键就返回真(True),否则返回假(Fal)。(Python 2.3版本以前,没有布尔常量,为真时返回1,假时返回0。)
>>> 'rver' in dict2 # or dict2.has_key('rver') Falr
>>> 'name' in dict # or dict2.has_key('name') True
>>> dict2['name']
'earth'
>>> 'rver' in dict2  # 或 dict2.has_key('rver')
Fal
祝福用语
>>> 'name' in dict  # 或 dict2.has_key('name')
True
>>> dict2['name']
'earth'
 Here is another dictionary example mixing the u of numbers and strings as keys:
  一个字典中混用数字和字符串的例子:
>>> dict3 = {}
>>> dict3[1] = 'abc'
五个能力
>>> dict3['1'] = 3.14159
>>> dict3[3.2] = 'xyz'
>>> dict3
{3.2: 'xyz', 1: 'abc', '1': 3.14159}
 Rather  than  adding  each  key-value  pair  individually,  we  could  have  also entered all the data for dict3 at the same time:
  除了逐一地添加每个键-值对外,我们也可以给dict3整体赋值。
dict3 = {3.2: 'xyz', 1: 'abc', '1': 3.14159}
 Creating the dictionary with a t key-value pair can be accomplished if all the data items are known in advance (obviously). The goal of the examples using dict3 is to illustrate the variety of keys that you can u. If we were to po the question of whether a key for a particular value should be allowed to change, you would probably say, “No.” Right? 如果事先已经知道所有的数据就可以用键-值对来创建一个字典(这是显而易见的)。通过字典dict3的示例说明你可以采用各种类型的数据作为字典的键。如果我们被问到是否可以改变某个字典值的键(key) 时,你可能会说,“不”,对吗?
 Not allowing keys to change during execution makes n if you think of it this way: Let us say that you created a dictionary element with a key and value. Somehow during execution of your program, the key changed, perhaps due to an altered variable. When you went to retrieve that data value again with the original key, you got a KeyError (since the key changed), and you had no idea how to obtain your value now becau the key had somehow been altered. For this reason, keys must be hashable, so numbers and strings are fine, but lists and other dictionaries are not
. (See Section 7.5.2 for why keys must be hashable.)
  为什么在执行中字典中的键不允许被改变呢?你这样想就会明白: 比方说, 你创建了一个字典,字典中包含一个元素(一个键和一个值)。可能是由于某个变量的改变导致键发生了改变。这时候你如果用原来的键来取出字典里的数据,会得到KeyError(因为键的值已经改变了),现在你没办法从字典中获取该值了,因为键本身的值发生了变化。由于上面的原因,字典中的键必须是可哈希的, 所以数字和字符串可以作为字典中的键, 但是列表和其他字典不行。(见7.5.2小节 字典的键必须是可哈希的)
How to Update Dictionaries
如何更新字典
You can update a dictionary by adding a new entry or element (i.e., a key-value pair),  modifying  an  existing  entry,  or  deleting  an  existing  entry  (e below for more details on removing an entry).
你可以通过以下几种方式对一个字典做修改:添加一个新数据项或新元素(即,一个键-值对);修改一个已存在的数据项;或删除一个已存在的数据项(下面有关于数据项删除操作的详细讲述).
>>> dict2['name'] = 'venus' # update existing entry
>>> dict2['port'] = 6969 # update existing entry
>>> dict2['arch'] = 'sunos5'# add new entry
>>>
>>> print 'host %(name)s is running on port %(port)d' %dict2
host venus is running on port 6969
>>> dict2['name'] = 'venus' # 更新已有条目
>>> dict2['port'] = 6969 # 更新已有条目
>>> dict2['arch'] = 'sunos5'# 增加新条目
>>>
>>> print 'host %(name)s is running on port %(port)d' %dict2
host venus is running on port 6969
 If the key does exist, then its previous value will be overridden by its new value. The print statement above illustrates an alternative way of using the string  format  operator  (  %  ),  specific  to  dictionaries.  Using  the  dictionary argument, you can shorten the print request somewhat becau naming of the dictionary occurs only once, as oppod to occurring for each element using a tuple argument.
  如果字典中该键已经存在,则字典中该键对应的值将被新值替代。上面的print语句展示了另一种在字典中使用字符串格式符( %)的方法。用字典参数可以简化print语句,因为这样做你只须用到一次该字典的名字,而不用在每个元素出现的时候都用元组参数表示。
  You may also add the contents of an entire dictionary to another dictionary by using the update() built-in method. We will introduce this method in Section 7.4.
  你也可以用内建方法update()将整个字典的内容添加到另一个字典。我们将在7.4节介绍此方法。
How to Remove Dictionary Elements and Dictionaries
如何删除字典元素和字典
Removing  an  entire  dictionary  is  not  a  typical  operation.  Generally,  you either remove individual dictionary elements or clear the entire contents o

本文发布于:2023-06-30 15:45:03,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/1061759.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:字典   类型   数据   映射   元素   方法   对象
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图