新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
Python程序:計(jì)算元組長(zhǎng)度
創(chuàng)新互聯(lián)Python教程:

寫(xiě)一個(gè) Python 程序來(lái)找到元組的長(zhǎng)度。在這個(gè)例子中,我們聲明了整數(shù)和字符串元組,并使用 Python len 函數(shù)來(lái)查找這些元組的長(zhǎng)度。
# Tuple Length
intTuple = (10, 20, 30, 40, 50)
print("Tuple Items = ", intTuple)
inttupleLength = len(intTuple)
print("Tuple Length = ", inttupleLength)
strTuple = ('apple', 'Mango', 'kiwi')
print("String Tuple Items = ", strTuple)
strtupleLength = len(strTuple)
print("String Tuple Length = ", strtupleLength)
Tuple Items = (10, 20, 30, 40, 50)
Tuple Length = 5
String Tuple Items = ('apple', 'Mango', 'kiwi')
String Tuple Length = 3
尋找元組長(zhǎng)度的 Python 程序
在這個(gè)例子中,我們聲明了嵌套和混合元組。如果您想要 Python 嵌套元組長(zhǎng)度,您必須使用該嵌套項(xiàng)的索引位置。例如,len(mTuple[4])返回嵌套元組(1,2,3,4) 的長(zhǎng)度
mTuple = ('Apple', 22, 'Kiwi', 45.6, (1, 2, 3, 4), 16, [10, 30, 70])
print("Mixed Tuple Items = ", mTuple)
mtupleLength = len(mTuple)
print("Mixed Tuple Length = ", mtupleLength)
nestedtupleLength = len(mTuple[4])
print("Nested Tuple Length = ", nestedtupleLength)
nestedlistLength = len(mTuple[6])
print("List Nested inside a Tuple Length = ", nestedlistLength)
在這個(gè) Python 程序中,我們聲明了一個(gè)空元組,并將用戶(hù)給定的值添加到該元組中,并計(jì)算長(zhǎng)度。
# Tuple Length
intTuple = ()
number = int(input("Enter the Total Tuple Items = "))
for i in range(1, number + 1):
value = int(input("Enter the %d value = " %i))
intTuple += (value,)
print("Tuple Items = ", intTuple)
inttupleLength = len(intTuple)
print("Tuple Length = ", inttupleLength)
Enter the Total Tuple Items = 4
Enter the 1 value = 22
Enter the 2 value = 99
Enter the 3 value = 128
Enter the 4 value = 65
Tuple Items = (22, 99, 128, 65)
Tuple Length = 4 名稱(chēng)欄目:Python程序:計(jì)算元組長(zhǎng)度
網(wǎng)址分享:http://www.dlmjj.cn/article/djpdcgs.html


咨詢(xún)
建站咨詢(xún)
