关于元组
元组是一个元素不可变的容器,这也就意味着,通过他们做hash算法总是得到一个值,而这恰恰是作为键值的必备条件,

交互解释器环境

Python 2.7.5 (default, Nov 20 2015, 02:00:19) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

元组的创建


一般创建方法

>>> atuple = (1,2,3,'string',['name','sex'],('Changer','man'),7-9j)
>>> type(atuple)
<type 'tuple'>
>>> print atuple
(1, 2, 3, 'string', ['name', 'sex'], ('Changer', 'man'), (7-9j))
>>> '123','ChangerLee',1,2,-4.2e+093
('123', 'ChangerLee', 1, 2, -4.2e+93)

单元素元组的创建

>>> test = (1,)
>>> type(test)
<type 'tuple'>
>>> print test
(1,)
>>> test = ('abc',)
>>> type(test)
<type 'tuple'>
>>> print test
('abc',)

常见误区:

>>> test = (1)
>>> type(test)
<type 'int'>
>>> test = ('abc')
>>> type(test)
<type 'str'>

类实例化创建:

class tuple(object)
 |  tuple() -> empty tuple
 |  tuple(iterable) -> tuple initialized from iterable's items

传入一个迭代器参数:字符串,列表,元组等

>>> test = tuple('abc')
>>> type(test)
<type 'tuple'>
>>> print test
('a', 'b', 'c')
>>> test = tuple('1')
>>> type(test)
<type 'tuple'>
>>> print test
('1',)
>>> type(tuple)
<type 'type'>
>>> test = tuple([1,2,3,3,'string',['name','sex'],('Changer','man'),7-9j])
>>> type(test)
<type 'tuple'>
>>> test
(1, 2, 3, 3, 'string', ['name', 'sex'], ('Changer', 'man'), (7-9j))
>>> test = tuple((1,2,3,3,'string',['name','sex'],('Changer','man'),7-9j))
>>> type(test)
<type 'tuple'>
>>> test
(1, 2, 3, 3, 'string', ['name', 'sex'], ('Changer', 'man'), (7-9j))

传入非迭代器会报错

>>> test = tuple(1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
>>> test = tuple(1,)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable

创建空元组

>>> test = tuple()
>>> type(test)
<type 'tuple'>
>>> print test
()
>>> test = ()
>>> type(test)
<type 'tuple'>
>>> print test
()

元组实例方法

count(...)
 |      T.count(value) -> integer --    return            
 |        number   of occurrences of value
 |  index(...)
 |      T.index(value, [start, [stop]]) -> integer -- return first index of value.
 |      Raises ValueError if the value is not present.

返回元组中指定元素的第一个下标

>>> test = tuple((1,2,3,3,'string',['name','sex'],('Changer','man'),7-9j))
>>> test.count(3)
2
>>> test.count(2)
1

统计元素在元组中的个数

>>> test.index(3)
2
>>> test.index('string')
4

元组的切片


>>> test[:]
(1, 2, 3, 3, 'string', ['name', 'sex'], ('Changer', 'man'), (7-9j))
>>> test[2:5]
(3, 3, 'string')
>>> test[5]
['name', 'sex']
>>> test[5][1]
'sex'
>>> test[6][1]
'man'
>>> test[7]
(7-9j)

元组的更新


添加元组元素

>>> test = tuple((1,2,3,3,'string',['name','sex'],('Changer','man'),7-9j))
>>> test = test+(1,2)
>>> test 
(1, 2, 3, 3, 'string', ['name', 'sex'], ('Changer', 'man'), (7-9j), 1, 2)

元组整体合并

>>> test = tuple((1,2,3,3,'string',['name','sex'],('Changer','man'),7-9j))
>>> test = test,('a',1,2)
>>> test 
((1, 2, 3, 3, 'string', ['name', 'sex'], ('Changer', 'man')), ('a', 1, 2))
>>> test[0] 
(1, 2, 3, 3, 'string', ['name', 'sex'], ('Changer', 'man'))
>>> test[1] 
(('a', 1, 2))

删除元组

>>> del test
>>> test
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'test' is not defined

注意:删除一个单独的元组元素是不可能的

>>> del test[7]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object doesn't support item deletion

可以通过切片重组元组

>>> test[:7]
(1, 2, 3, 3, 'string', ['name', 'sex'], ('Changer', 'man'))
>>> test = test[:7]
>>> test 
(1, 2, 3, 3, 'string', ['name', 'sex'], ('Changer', 'man'))

元组的操作符


成员关系操作符 in ,not in

>>> test
(1, 2, 3, 3, 'string', ['name', 'sex'], ('Changer', 'man'), (7-9j))
>>> 'string' in test
True
>>> 5 not in test
True

连接,重复操作符+,*

>>> test = (1,2,3,4)+(5,6,7,8,9)
>>> test 
(1, 2, 3, 4, 5, 6, 7, 8, 9)
>>> test+('a','b','c') 
(1, 2, 3, 4, 5, 6, 7, 8, 9, 'a', 'b', 'c')
>>> test = (1,2,3,4)
>>> test 
(1, 2, 3, 4)
>>> test = (1,2,3,4)*3
>>> test
(1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4)

逻辑操作符< = >
注意:只针对元组的第一个元素做逻辑运算

>>> (4,2) > (3,4)
True
>>> (1,2) > (3,1)
False
>>> (1,2) == (3,4)
False
>>> (1,2) == (1,4)
False

比较中间的元素,将返回的布尔值插入后建立的元组

>>> 1,2 > 3,4
(1, False, 4)
>>> 4,2 == 2,4
(4, True, 4)
>>> 4,2 < 1,4
(4, False, 4)

4,3 > 1,4
(4, True, 4)

```
>>> 4,3 > 1,4,8
(4, True, 4, 8)
>>> 1,3,4,3 > 1,4,8
(1, 3, 4, True, 4, 8)

常用内建函数对元组的操作
更新不可变的元组时,先转换成列表,在调取列表方法更新,也是个不错的选择
list()

>>> test
(1, 2, 3, 4)
>>> test = list(test)
>>> test
[1, 2, 3, 4]
>>> test = tuple(test)
>>> test
(1, 2, 3, 4)

len()

>>> len(test)
4
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐