Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1798332
  • 博文数量: 335
  • 博客积分: 4690
  • 博客等级: 上校
  • 技术积分: 4341
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-08 21:38
个人简介

无聊之人--除了技术,还是技术,你懂得

文章分类

全部博文(335)

文章存档

2016年(29)

2015年(18)

2014年(7)

2013年(86)

2012年(90)

2011年(105)

分类: Python/Ruby

2011-06-13 21:29:58

Chapter 3. Native Datatypes

原生数据类型

You'll get back to your first Python program in just a minute. But first, a short digression is in order, because you need to know about dictionaries, tuples, and lists (oh my!). If you're a Perl hacker, you can probably skim the bits about dictionaries and lists, but you should still pay attention to tuples.

稍后我们将继续讨论第一个介绍的Python程序.但是首先,顺序略为有点打乱,你应该知道字典,不变列表 以及列表(,我的天哪).如果你是一个Perl高手,你可以掠过列表和字典的相关知识,但是对于不变列表你仍需要重视.

3.1. Introducing Dictionaries

One of Python's built-in datatypes is the dictionary, which defines one-to-one relationships between keys and values.

Note

 

A dictionary in Python is like a hash in Perl. In Perl, variables that store hashes always start with a % character. In Python, variables can be named anything, andPython keeps track of the datatype internally.

 

Note

 

A dictionary in Python is like an instance of the Hashtable class in Java.

 

Note

 

A dictionary in Python is like an instance of the Scripting.Dictionary object in Visual Basic.

 

3.1.1. Defining Dictionaries

字典的定义

Example 3.1. Defining a Dictionary

3.1 定义一个字典

  1. >>> d = {"server":"mpilgrim", "database":"master"} 
  2. >>> d
  3. {'server': 'mpilgrim', 'database': 'master'}
  4. >>> d["server"] 
  5. 'mpilgrim'
  6. >>> d["database"] 
  7. 'master'
  8. >>> d["mpilgrim"] 
  9. Traceback (innermost last):
  10.   File "", line 1, in ?
  11. KeyError: mpilgrim

1

First, you create a new dictionary with two elements and assign it to the variable d. Each element is a key-value pair, and the whole set of elements is enclosed in curly braces.

首先,创建一个包含两个元素的新字典,并将它赋值给变量d.每一个元素都是一个键-值对,全部元素使用大括号来封装起来。

2

'server' is a key, and its associated value, referenced by d["server"], is 'mpilgrim'.

server是一个键,它同一个值相关联,通过d[‘server’]来引用,该值为’mpilgrim’.

3

'database' is a key, and its associated value, referenced by d["database"], is 'master'.

‘database’为一个键,通过d[‘database’]来引用,与值‘master‘相关联。

4

You can get values by key, but you can't get keys by value. So d["server"] is 'mpilgrim', but d["mpilgrim"] raises an exception, because 'mpilgrim' is not a key.

可以通过键来获取值,但不能通过值来获取键。因此,d[‘server’]的值为’mpilgrim‘,但是d[‘mpilgrim’]却引发了一个异常,这是因为‘mpilgrim’不是键。

3.1.2. Modifying Dictionaries

字典修改

Example 3.2. Modifying a Dictionary

3.2 修改一个字典

  1. >>> d
  2. {'server': 'mpilgrim', 'database': 'master'}
  3. >>> d["database"] = "pubs" 
  4. >>> d
  5. {'server': 'mpilgrim', 'database': 'pubs'}
  6. >>> d["uid"] = "sa" 
  7. >>> d
  8. {'server': 'mpilgrim', 'uid': 'sa', 'database': 'pubs'}

1

You can not have duplicate keys in a dictionary. Assigning a value to an existing key will wipe out the old value.

在字典中键不能重复。给一个已经存在的键重新赋值将覆盖原来的值。

2

You can add new key-value pairs at any time. This syntax is identical to modifying existing values. (Yes, this will annoy you someday when you think you are adding new values but are actually just modifying the same value over and over because your key isn't changing the way you think it is.)

你可以随时给字典增加键值对,语法同修改已经存在的键的值方法是一样的(当然,有一天当你想增加一个键值,但是实际上你却修改了一个键值,这样反反复复,键不是按你想要的方式进行修改时,你可能非常恼怒)

Note that the new element (key 'uid', value 'sa') appears to be in the middle. In fact, it was just a coincidence that the elements appeared to be in order in the first example; it is just as much a coincidence that they appear to be out of order now.

需要注意的是,(键为“uid”,值为”sa”)的元素在字典里面的位置看起来是在中间。但是事实上,第一个例子中元素在字典中的位置在中间只是一个巧合;按这种顺序出现和不按这种顺序出现的概率是相等的。

Note

 

Dictionaries have no concept of order among elements. It is incorrect to say that the elements are “out of order”; they are simply unordered. This is an important distinction that will annoy you when you want to access the elements of a dictionary in a specific, repeatable order (like alphabetical order by key). There are ways of doing this, but they're not built into the dictionary.

字典中的元素之间没有顺序的概念。那种说字典中的元素没有顺序也是不正确的,他们只是无序。当你想你某种特定的,可重复的顺序(如按字母顺序)来访问字典中的元素时,这种重要的特性可能回让你不高兴。有好几种方式可以实现这种效果,但这些特性没有内置到字典中。

 

When working with dictionaries, you need to be aware that dictionary keys are case-sensitive.

当你是使用字典时,有一点需要注意:字典的键是大小写敏感的。

Example 3.3. Dictionary Keys Are Case-Sensitive

3.3 字典的键大小写敏感

  1. >>> d = {}
  2. >>> d["key"] = "value"
  3. >>> d["key"] = "other value" 
  4. >>> d
  5. {'key': 'other value'}
  6. >>> d["Key"] = "third value" 
  7. >>> d
  8. {'Key': 'third value', 'key': 'other value'}

1

Assigning a value to an existing dictionary key simply replaces the old value with a new one.

给一个已经存在键重新赋值将简单的覆盖掉原来的值。

2

This is not assigning a value to an existing dictionary key, because strings in Python are case-sensitive, so 'key' is not the same as 'Key'. This creates a new key/value pair in the dictionary; it may look similar to you, but as far as Python is concerned, it's completely different.

这不是给一个已经存在的键重新赋值,这是因为在Python中字符串是大小写敏感的,‘key’’Key‘是不同的。这就在字典中创建了一个新的键值对;这两个键,你可能看起来差不多,但是从Python的角度而言,它们是完全不同的。

Example 3.4. Mixing Datatypes in a Dictionary

3.4 字典中可以混合不同的数据类型

  1. >>> d
  2. {'server': 'mpilgrim', 'uid': 'sa', 'database': 'pubs'}
  3. >>> d["retrycount"] = 3 
  4. >>> d
  5. {'server': 'mpilgrim', 'uid': 'sa', 'database': 'master', 'retrycount': 3}
  6. >>> d[42] = "douglas" 
  7. >>> d
  8. {'server': 'mpilgrim', 'uid': 'sa', 'database': 'master',
  9. 42: 'douglas', 'retrycount': 3}

1

Dictionaries aren't just for strings. Dictionary values can be any datatype, including strings, integers, objects, or even other dictionaries. And within a single dictionary, the values don't all need to be the same type; you can mix and match as needed.

字典不仅仅使用于字符串。字典值可以为任意数据类型,包括字符串,整型,对象甚至是其它字典。在一个简单字典内,值得数据类型不必都相同,你可以根据需要来进行搭配。

2

Dictionary keys are more restricted, but they can be strings, integers, and a few other types. You can also mix and match key datatypes within a dictionary.

字典的键是十分严格,它们的类型可以是字符串,整数,以及其它少数几种数据类型。在字典内你可以自己搭配键的数据类型。

3.1.3. Deleting Items From Dictionaries

删除字典中的项(键值对)

Example 3.5. Deleting Items from a Dictionary

3.5 从字典中删除项

  1. >>> d
  2. {'server': 'mpilgrim', 'uid': 'sa', 'database': 'master',
  3. 42: 'douglas', 'retrycount': 3}
  4. >>> del d[42] 
  5. >>> d
  6. {'server': 'mpilgrim', 'uid': 'sa', 'database': 'master', 'retrycount': 3}
  7. >>> d.clear() 
  8. >>> d
  9. {}

1

del lets you delete individual items from a dictionary by key.

Del 让你从字典根据键来删除单独的一个项。

2

clear deletes all items from a dictionary. Note that the set of empty curly braces signifies a dictionary without any items.

Clear 清空字典。需要注意的是,空的大括号说明了一个字典没有任何项。


阅读(978) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~