新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Python程序:打印奇數(shù)位置的列表項
創(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è)經驗、深度合作伙伴關系等,向廣大中小型企業(yè)、政府機構等提供互聯(lián)網(wǎng)行業(yè)的解決方案,蓮池網(wǎng)站推廣取得了明顯的社會效益與經濟效益。目前,我們服務的客戶以成都為中心已經輻射到蓮池省份的部分城市,未來相信會繼續(xù)擴大服務區(qū)域并繼續(xù)獲得客戶的支持與信任!
寫一個 Python 程序,打印奇數(shù)位置或奇數(shù)索引位置的列表項。在這個 Python 示例中,我們使用列表切片,從 0 開始,遞增 2,以列表長度(列表的末尾)結束。
odList = [2, 4, 7, 11, 14, 22, 19, 90]
print('Printing the List Items at Odd Position')
print(odList[0:len(odList):2])在這個 Python 程序中,我們使用 for 循環(huán)范圍來迭代列表項,并在奇數(shù)索引位置打印列表項。
odList = [36, 48, 77, 55, 90, 128, 193, 240]
print('Printing the List Items at Odd Position')
for i in range(0, len(odList), 2):
print(odList[i], end = ' ')Printing the List Items at Odd Position
36 77 90 193 Python 程序使用 while 循環(huán)打印奇數(shù)位置的列表項
odList = [14, 35, 78, 90, 120, 67, 98]
print('Printing the List Items at Odd Position')
i = 0
while i < len(odList):
print(odList[i], end = ' ')
i = i + 2Printing the List Items at Odd Position
14 78 120 98 在本 Python 列表中奇數(shù)位置的項目示例中,for 循環(huán)從 0 迭代到列表長度。if 條件檢查索引位置除以二是否等于 0。如果為真,打印數(shù)字。
import numpy as np
odlist = []
odListTot = int(input("Total List Items to enter = "))
for i in range(1, odListTot + 1):
odListvalue = int(input("Please enter the %d List Item = " %i))
odlist.append(odListvalue)
print('\nPrinting the List Items at Odd Position')
for i in range(0, len(odlist), 2):
print(odlist[i], end = ' ')
print('\nPrinting the List Items at Odd Position')
for i in range(len(odlist)):
if i % 2 == 0:
print(odlist[i], end = ' ')Total List Items to enter = 8
Please enter the 1 List Item = 14
Please enter the 2 List Item = 24
Please enter the 3 List Item = 34
Please enter the 4 List Item = 44
Please enter the 5 List Item = 54
Please enter the 6 List Item = 64
Please enter the 7 List Item = 74
Please enter the 8 List Item = 84
Printing the List Items at Odd Position
14 34 54 74
Printing the List Items at Odd Position
14 34 54 74 新聞名稱:Python程序:打印奇數(shù)位置的列表項
URL鏈接:http://m.5511xx.com/article/cdjdogd.html


咨詢
建站咨詢
