新聞中心
元組是不同數(shù)據(jù)類型的元素的不可變(不可改變)集合。這是一個(gè)有序集合,因此它保留了元素定義的順序。

元組由括號(hào)()中的元素定義,用逗號(hào)分隔。 下面聲明一個(gè)元組類型變量。
Example: Tuple Variable Declaration
tpl=() # empty tuple
print(tpl)
names = ('Jeff', 'Bill', 'Steve', 'Yash') # string tuple
print(names)
nums = (1, 2, 3, 4, 5) # int tuple
print(nums)
employee=(1, 'Steve', True, 25, 12000) # heterogeneous data tuple
print(employee)
Output:
()
('Jeff', 'Bill', 'Steve', 'Yash')
(1, 2, 3, 4, 5)
(1, 'Steve', True, 25, 12000)
但是,不必將元組元素括在括號(hào)中。元組對(duì)象可以包括由逗號(hào)分隔的元素,沒(méi)有括號(hào)。
Example: Tuple Variable Declaration
names = 'Jeff', 'Bill', 'Steve', 'Yash' # string tuple
print(names)
nums = 1, 2, 3, 4, 5 # int tuple
print(nums)
employee=1, 'Steve', True, 25, 12000 # heterogeneous data tuple
print(employee)
Output:
('Jeff', 'Bill', 'Steve', 'Yash')
(1, 2, 3, 4, 5)
(1, 'Steve', True, 25, 12000)
除非后跟逗號(hào),否則元組不能用單個(gè)元素聲明。
Example: Tuple Variable Declaration
names = ('Jeff') # considered as string type
print(names)
print(type(names))
names = ('Jeff',) # tuple with single element
print(names)
print(type(names))
Output:
'Jeff'
(Jeff)
訪問(wèn)元組元素
元組中的每個(gè)元素都由方括號(hào)[]中的索引訪問(wèn)。索引以零開(kāi)始,以(元素?cái)?shù)- 1)結(jié)束,如下所示。
Example: Access Tuple Elements using Indexes
names = ('Jeff', 'Bill', 'Steve', 'Yash')
print(names[0]) # prints 'Jeff'
print(names[1]) # prints 'Bill'
print(names[2]) # prints 'Steve'
print(names[3]) # prints 'Yash'
nums = (1, 2, 3, 4, 5)
print(nums[0]) # prints 1
print(nums[1]) # prints 2
print(nums[4]) # prints 5
Output:
Jeff
Bill
Steve
Yash
1
2
5
元組也支持負(fù)索引,與列表類型相同。第一個(gè)元素的負(fù)指數(shù)從-number of elements開(kāi)始,最后一個(gè)元素以-1 結(jié)束。
Example: Negative Indexing
names = ('Jeff', 'Bill', 'Steve', 'Yash')
print(names[-4]) # prints 'Jeff'
print(names[-3]) # prints 'Bill'
print(names[-2]) # prints 'Steve'
print(names[-1]) # prints 'Yash'
Output:
Jeff
Bill
Steve
Yash
如果指定索引處的元素不存在,則將引發(fā)錯(cuò)誤“索引超出范圍”。
>>> names[5]
Traceback (most recent call last):
File "", line 1, in
IndexError: tuple index out of range
元組元素可以被解包并分配給變量,如下所示。但是,變量的數(shù)量必須與元組中元素的數(shù)量相匹配;否則,將引發(fā)錯(cuò)誤。
Example: Access Tuple Elements using Indexes
names = ('Jeff', 'Bill', 'Steve', 'Yash')
a, b, c, d = names # unpack tuple
print(a, b, c, d)
Output:
Jeff Bill Steve Yash
更新或刪除元組元素
元組是不可更改的。因此,一旦創(chuàng)建了元組,任何試圖改變其內(nèi)容的操作都是不允許的。例如,試圖修改或刪除names元組的元素將導(dǎo)致錯(cuò)誤。
>>> names = ('Jeff', 'Bill', 'Steve', 'Yash')
>>> names[0] = 'Swati'
Traceback (most recent call last):
File "", line 1, in
TypeError: 'tuple' object does not support item assignment
>>> del names[0]
Traceback (most recent call last):
File "", line 1, in
TypeError: 'tuple' object doesn't support item deletion
但是,您可以使用del關(guān)鍵字刪除整個(gè)元組。
>>> del names
元組類
元組的基礎(chǔ)類型是元組類。使用type()功能檢查變量的類型。
Example: Tuple Variable Declaration
names = ('Jeff', 'Bill', 'Steve', 'Yash')
print('names type: ', type(names))
nums = (1,2,3,4,5)
print('nums type: ', type(nums))
Output:
names type:
nums type:
tuple()構(gòu)造器用于將任何可迭代類型轉(zhuǎn)換為元組類型。
Example: Tuple Variable Declaration
tpl = tuple('Hello') # converts string to tuple
print(tpl)
tpl = tuple([1,2,3,4,5]) # converts list to tuple
print(tpl)
tpl = tuple({1,2,3,4,5}) # converts set to tuple
print(tpl)
tpl = tuple({1:"One",2:"Two"}) # converts dictionary to tuple
print(tpl)
Output:
('H','e','l','l','o')
(1,2,3,4,5)
(1,2,3,4,5)
(1,2)
元組運(yùn)算
像字符串一樣,元組對(duì)象也是一個(gè)序列。因此,用于字符串的運(yùn)算符也可用于元組。
| 操作員 | 例子 |
|---|---|
| + 運(yùn)算符返回包含第一個(gè)和第二個(gè)元組對(duì)象的所有元素的元組。 |
>>> t1=(1,2,3)
>>> t2=(4,5,6)
>>> t1+t2
(1, 2, 3, 4, 5, 6)
>>> t2+(7,)
(4, 5, 6, 7)
| | ***** 運(yùn)算符連接同一個(gè)元組的多個(gè)副本。 |
>>> t1=(1,2,3)
>>> t1*4
(1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3)
| | [] 運(yùn)算符返回給定索引處的項(xiàng)目。負(fù)索引從右側(cè)開(kāi)始計(jì)算位置。 |
>>> t1=(1,2,3,4,5,6)
>>> t1[3]
4
>>> t1[-2]
5
| | [:] 運(yùn)算符返回由兩個(gè)索引操作數(shù)指定的范圍內(nèi)的項(xiàng)目,這兩個(gè)索引操作數(shù)由:符號(hào)分隔。 如果省略第一個(gè)操作數(shù),范圍從零開(kāi)始。 如果省略第二個(gè)操作數(shù),范圍將上升到元組的末尾。 |
>>> t1=(1,2,3,4,5,6)
>>> t1[1:3]
(2, 3)
>>> t1[3:]
(4, 5, 6)
>>> t1[:3]
(1, 2, 3)
| | 如果給定元組中存在某項(xiàng),則運(yùn)算符中的返回真。 |
>>> t1=(1,2,3,4,5,6)
>>> 5 in t1
True
>>> 10 in t1
False
| | 如果給定元組中不存在某項(xiàng),則不在運(yùn)算符中的返回真。 |
>>> t1=(1,2,3,4,5,6)
>>> 4 not in t1
False
>>> 10 not in t1
True
|
當(dāng)前題目:Python元組
分享鏈接:http://fisionsoft.com.cn/article/cogscho.html


咨詢
建站咨詢
