Python元組詳解
阿新 • • 發佈:2019-01-06
關於元組
元組是一個元素不可變的容器,這也就意味著,通過他們做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