Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1753408
  • 博文数量: 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-14 00:58:15

3.3. Introducing Tuples

A tuple is an immutable list. A tuple can not be changed in any way once it is created.

不变列表是不能改变的列表。不变列表一旦创建,就不能通过任何方式改变

Example 3.15. Defining a tuple

3.15 定义一个不变列表

  1. >>> t = ("a", "b", "mpilgrim", "z", "example")
  2. >>> t
  3. ('a', 'b', 'mpilgrim', 'z', 'example')
  4. >>> t[0]
  5. 'a'
  6. >>> t[-1]
  7. 'example'
  8. >>> t[1:3]
  9. ('b', 'mpilgrim')

1

A tuple is defined in the same way as a list, except that the whole set of elements is enclosed in parentheses instead of square brackets.

不变列表的定义同列表的定义类似,除了不变列表的元素集通过括号来封装而不是使用方括号。

2

The elements of a tuple have a defined order, just like a list. Tuples indices are zero-based, just like a list, so the first element of a non-empty tuple is always t[0].

不变列表的元素也有一个定义时顺序,同列表类似。不变列表也是从0开始索引,同列表一样,因此非空列表的第一个元素总是Li[0].

3

Negative indices count from the end of the tuple, just as with a list.

同列表类似,不变列表的反向索引也是从反向开始计数索引元素的

4

Slicing works too, just like a list. Note that when you slice a list, you get a new list; when you slice a tuple, you get a new tuple.

切片同样使用与不变列表。值得注意的是,当你对一个列表进行切片时,返回值为一个新列表,当你对不变列表进行切片时,你得到一个tuple.

Example 3.16. Tuples Have No Methods

3.16 tuples不具有任何方法

  1. >>> t
  2. ('a', 'b', 'mpilgrim', 'z', 'example')
  3. >>> t.append("new")
  4. Traceback (innermost last):
  5.   File "", line 1, in ?
  6. AttributeError: 'tuple' object has no attribute 'append'
  7. >>> t.remove("z")
  8. Traceback (innermost last):
  9.   File "", line 1, in ?
  10. AttributeError: 'tuple' object has no attribute 'remove'
  11. >>> t.index("example")
  12. Traceback (innermost last):
  13.   File "", line 1, in ?
  14. AttributeError: 'tuple' object has no attribute 'index'
  15. >>> "z" in t
  16. True

1

You can't add elements to a tuple. Tuples have no append or extend method.

Tuple不能添加元素,同样它也不具有append或是extend方法。

2

You can't remove elements from a tuple. Tuples have no remove or pop method.

你不能从tuple中移除元素,它也没有remove或是pop方法。

3

You can't find elements in a tuple. Tuples have no index method.

你不在能tuple中查找元素,不具有index方法。

4

You can, however, use in to see if an element exists in the tuple.

然而你可以用 in 来测试tuple是否存在一个元素。

So what are tuples good for?

那么tuple有什么好处呢?

  • Tuples are faster than lists. If you're defining a constant set of values and all you're ever going to do with it is iterate through it, use a tuple instead of a list.
  • Tuple的速度比list要快。如果你打算定义一个常量值的集合并且你将来唯一想做的就是迭代争个tuple,那么请采用tuple而不是列表。
  • It makes your code safer if you “write-protect” data that does not need to be changed. Using a tuple instead of a list is like having an implied assert statement that shows this data is constant, and that special thought (and a specific function) is required to override that.
  • 如果你的写保护数据不需要任何修改,使用tuple将使你的代码更加安全。采用tuple而不是列表就如同你拥有一个断言语句,该语句断言这些数据是常量,只有经过深思熟略(或是一个特殊的函数)才能覆盖掉它。
  • Remember that I said that dictionary keys can be integers, strings, and “a few other types”? Tuples are one of those types. Tuples can be used as keys in a dictionary, but lists can't be used this way.Actually, it's more complicated than that. Dictionary keys must be immutable. Tuples themselves are immutable, but if you have a tuple of lists, that counts as mutable and isn't safe to use as a dictionary key. Only tuples of strings, numbers, or other dictionary-safe tuples can be used as dictionary keys.
  • 记得我曾说过字典的键可以为整数,字符串还有少数其它几种数据类型。Tuple就是这几种类型之一。Tuple可以用作字典的键,但是列表却不能这么用。事实上事情很复杂。字典的键是不能修改的。Tuple本身是不可修改的,但是如果你有一个list类型的tuple(???),它可以被修改,用作字典的键就不安全。只有字符串类型的字典,整数类型的字典以及其他字典安全的tuple才可以用作字典的键。
  • Tuples are used in string formatting, as you'll see shortly.
  • Tuple可以用来格式化字符串,稍后你将看到

Note

 

Tuples can be converted into lists, and vice-versa. The built-in tuple function takes a list and returns a tuple with the same elements, and the list function takes a tuple and returns a list. In effect, tuple freezes a list, and list thaws a tuple.tuple和列表可以相互转化。内置tuple函数将一个列表转换成一个含有相同元素的tuple,而list函数则将一个tuple转换成一个列表。事实上tuple冻结了listlist解冻了了一个tuple

 

 

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