映射和集合类型

上传人:子 文档编号:42837914 上传时间:2018-06-03 格式:DOC 页数:15 大小:23.35KB
返回 下载 相关 举报
映射和集合类型_第1页
第1页 / 共15页
映射和集合类型_第2页
第2页 / 共15页
映射和集合类型_第3页
第3页 / 共15页
映射和集合类型_第4页
第4页 / 共15页
映射和集合类型_第5页
第5页 / 共15页
点击查看更多>>
资源描述

《映射和集合类型》由会员分享,可在线阅读,更多相关《映射和集合类型(15页珍藏版)》请在金锄头文库上搜索。

1、映射和集合类型映射和集合类型Mapping and Set Types映射和集合类型Chapter TopicsMapping Type: DictionariesOperatorsBuilt-in FunctionsBuilt-in MethodsDictionary KeysSet TypesOperatorsBuilt-in FunctionsBuilt-in MethodsRelated Modules7*本章主题* 映射类型: 字典* 操作符* 内建函数* 内建方法* 字典的键* 集合类型* 操作符* 内建函数* 内建方法* 相关模块第七章in this chapter, we ta

2、ke a look at Pythons mapping and set 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 语言中的映射类型和集合类型。和前面的章节一样,我们首先做一个介绍,然

3、后在来讨论可用操作符,工厂函数、内建函数(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 represent (values). They are similar to Perl hashes and can be

4、 generally considered as mutable hash tables. A dictionary object itself 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 sequence type containers like lists and tuples is the way the dat

5、a are stored and accessed. Sequence types use numeric keys only (numbered sequentially as indexed offsets from the beginning of the sequence). Mapping types may use most other object types as keys; strings are the most common. Unlike sequence type keys, mapping keys are often, if not directly, assoc

6、iated with the data value that is stored. But because we are no longer using “sequentially ordered” keys with mapping types, we are left with an unordered collection of data. 字典是 Python 语言中唯一的映射类型。映射类型对象里哈希值(键) 和指向的对象(值)是一对多的关系。 它们与 Perl 中的哈希类型(译者注:又称关联数组)相似,通常被认为是可变的哈希表。一个字典对象是可变的,它是一个容器类型,能存储任意个数的

7、 Python 对象,其中也包括其他容器类型。字典类型和序列类型容器类(列表、元组)的区别是存储和访问数据的方式不同。序列类型只用数字类型的键(从序列的开始起按数值顺序索引)。映射类型可以用其他对象类型做键;一般最常见的是用字符串做键(keys)。和序列类型的键不同,映射类型的键(keys)直接,或间接地和存储的数据值相关联。但因为在映射类型中,我们不再用“序列化排序“的键(keys),所以映射类型中的数据是无序排列的。As it turns out, this does not hinder our use because mapping types do not require a num

8、eric 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 because that is the exact type of object that dictionaries are. Dictionaries are one of

9、Pythons most powerful data types. 显然,这并不影响我们使用映射类型,因为映射类型不要求用数字值做索引以从一个容器中获取对应的数据项。你可以用键(key)直接 “映射“ 到值, 这就是为什么叫映射类型(“mapping type”)的原因。映射类型通常被称做哈希表的原因是字典对象就是哈希类型的。字典是 Python 中最强大的数据类型之一。CORE NOTE: What are hash tables and how do they relate to dictionaries?核心笔记:什么是哈希表?它们与字典的关系是什么?Sequence types use

10、 sequentially ordered numeric keys as index offsets to store your data in an array format. The index number usually has nothing to do with the data value that is being stored. There should also be a way to store data based on another associated value such as a string. We do this all the time in ever

11、yday living. You file peoples phone numbers in your address book based on last name, you add events to your calendar or appointment book based on date and time, etc. For each of these examples, an associated value to a data item was your key.序列类型用有序的数字键做索引将数据以数组的形式存储。一般,索引值与所存储的数据毫无关系。还可以用另一种方式来存储数据

12、:基于某种相关值,比如说一个字符串。我们在日常生活中一直这么做。你把人们的电话号码按照他们的姓记录在电话簿上,你按照时间在日历或约会簿上添加事件,等等。在这些例子中,你的键(key)就是和数据项相关的值。Hash tables are a data structure that does exactly what we described. They store each piece of data, called a value, based on an associated data item, called a key. Together, these are known as key-

13、value pairs. The hash table algorithm takes your key, performs an operation on it, called a hash function, and based on the result of the calculation, chooses where in the data structure to store your value. Where any one particular value is stored depends on what its key is. Because of this randomn

14、ess, there is no ordering of the values in the hash table. You have an unordered collection of data.哈希表是一种数据结构:它按照我们所要求的去工作。哈希表中存储的每一条数据,叫做一个值(value),是根据与它相关的一个被称作为键(key)的数据项进行存储的。键和值合在一起被称为“键-值 对”(key-value pairs)。 哈希表的算法是获取键,对键执行一个叫做哈希函数的操作,并根据计算的结果,选择在数据结构的某个地址中来存储你的值。任何一个值存储的地址皆取决于它的键。正因为这种随意性,哈

15、希表中的值是没有顺序的。你拥有的是一个无序的数据集。The only kind of ordering you can obtain is by taking either a dictionarys set 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 themselves

16、 have no implicit ordering because they are hashes.你所能获得的有序集合只能是字典中的键的集合或者值的集合。方法 Keys() 或 values() 返回一个列表,该列表是可排序的。 你还可以用 items()方法得到包含键、值对的元组的列表来排序。由于字典本身是哈希的,所以是无序的。Hash tables generally provide good performance because 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 Perls associative arrays or hashes.Python 的字典是作为可变的哈希表实现

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 生活休闲 > 科普知识

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号