Python 程序:打印奇數位置的列表項

寫一個 Python 程序,打印奇數位置或奇數索引位置的列表項。在這個 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 循環範圍來迭代列表項,並在奇數索引位置打印列表項。

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 
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 + 2
Printing the List Items at Odd Position
14  78  120  98 

在本 Python 列表中奇數位置的項目示例中,for 循環從 0 迭代到列表長度。if 條件檢查索引位置除以二是否等於 0。如果為真,打印數字。

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 

原創文章,作者:D4H5T,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/127025.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
D4H5T的頭像D4H5T
上一篇 2024-10-03 23:13
下一篇 2024-10-03 23:13

相關推薦

  • python強行終止程序快捷鍵

    本文將從多個方面對python強行終止程序快捷鍵進行詳細闡述,並提供相應代碼示例。 一、Ctrl+C快捷鍵 Ctrl+C快捷鍵是在終端中經常用來強行終止運行的程序。當你在終端中運行…

    編程 2025-04-29
  • Python程序需要編譯才能執行

    Python 被廣泛應用於數據分析、人工智能、科學計算等領域,它的靈活性和簡單易學的性質使得越來越多的人喜歡使用 Python 進行編程。然而,在 Python 中程序執行的方式不…

    編程 2025-04-29
  • ArcGIS更改標註位置為中心的方法

    本篇文章將從多個方面詳細闡述如何在ArcGIS中更改標註位置為中心。讓我們一步步來看。 一、禁止標註智能調整 在ArcMap中設置標註智能調整可以自動將標註位置調整到最佳顯示位置。…

    編程 2025-04-29
  • Python字符轉列表指南

    Python是一個極為流行的腳本語言,在數據處理、數據分析、人工智能等領域廣泛應用。在很多場景下需要將字符串轉換為列表,以便於操作和處理,本篇文章將從多個方面對Python字符轉列…

    編程 2025-04-29
  • Python程序文件的拓展

    Python是一門功能豐富、易於學習、可讀性高的編程語言。Python程序文件通常以.py為文件拓展名,被廣泛應用於各種領域,包括Web開發、機器學習、科學計算等。為了更好地發揮P…

    編程 2025-04-29
  • Python中不同類型的列表

    Python是一種功能強大的編程語言,其內置數據結構之一為列表。列表可以容納任意數量的元素,並且可以存儲不同類型的數據。 一、列表的基本操作 Python的列表類型支持許多操作,如…

    編程 2025-04-29
  • Python購物車程序

    Python購物車程序是一款基於Python編程語言開發的程序,可以實現購物車的相關功能,包括商品的添加、購買、刪除、統計等。 一、添加商品 添加商品是購物車程序的基礎功能之一,用…

    編程 2025-04-29
  • 爬蟲是一種程序

    爬蟲是一種程序,用於自動獲取互聯網上的信息。本文將從如下多個方面對爬蟲的意義、運行方式、應用場景和技術要點等進行詳細的闡述。 一、爬蟲的意義 1、獲取信息:爬蟲可以自動獲取互聯網上…

    編程 2025-04-29
  • Vb運行程序的三種方法

    VB是一種非常實用的編程工具,它可以被用於開發各種不同的應用程序,從簡單的計算器到更複雜的商業軟件。在VB中,有許多不同的方法可以運行程序,包括編譯器、發佈程序以及命令行。在本文中…

    編程 2025-04-29
  • Python為什麼輸出空列表

    空列表是Python編程中常見的數據類型,在某些情況下,會出現輸出空列表的情況。下面我們就從多個方面為大家詳細闡述為什麼Python會輸出空列表。 一、賦值錯誤 在Python中,…

    編程 2025-04-29

發表回復

登錄後才能評論