新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Python 程序:從元組中移除項目
創(chuàng)新互聯(lián)python教程:

創(chuàng)新互聯(lián)建站服務項目包括政和網(wǎng)站建設、政和網(wǎng)站制作、政和網(wǎng)頁制作以及政和網(wǎng)絡營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關系等,向廣大中小型企業(yè)、政府機構等提供互聯(lián)網(wǎng)行業(yè)的解決方案,政和網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟效益。目前,我們服務的客戶以成都為中心已經(jīng)輻射到政和省份的部分城市,未來相信會繼續(xù)擴大服務區(qū)域并繼續(xù)獲得客戶的支持與信任!
編寫一個 Python 程序,從元組中移除或刪除一個項目。在 Python 中,我們不能從元組中刪除一個項目。相反,我們必須將其分配給一個新的元組。在這個例子中,我們使用元組切片和連接來移除元組項。第一個,numtup[:3]+numtup[4:]刪除了第三個元組項。
# Remove an Item from Tuple
numTuple = (9, 11, 22, 45, 67, 89, 15, 25, 19)
print("Tuple Items = ", numTuple)
numTuple = numTuple[:3] + numTuple[4:]
print("After Removing 4th Tuple Item = ", numTuple)
numTuple = numTuple[:5] + numTuple[7:]
print("After Removing 5th and 6th Tuple Item = ", numTuple)Python 中的另一個選項是將 Tuple 轉換為列表,并使用移除函數(shù),而不是移除列表項。接下來,轉換回元組。
# Remove an Item from Tuple
numTuple = (2, 22, 33, 44, 5, 66, 77)
print("Tuple Items = ", numTuple)
numList = list(numTuple)
numList.remove(44)
numTuple1 = tuple(numList)
print("After Removing 3rd Tuple Item = ", numTuple1)Tuple Items = (2, 22, 33, 44, 5, 66, 77)
After Removing 3rd Tuple Item = (2, 22, 33, 5, 66, 77) 分享名稱:Python 程序:從元組中移除項目
網(wǎng)站鏈接:http://fisionsoft.com.cn/article/dhhidpe.html


咨詢
建站咨詢
