新聞中心
創(chuàng)新互聯(lián)python教程:

創(chuàng)新互聯(lián)是一家專業(yè)提供鹽池企業(yè)網(wǎng)站建設(shè),專注與網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計、成都h5網(wǎng)站建設(shè)、小程序制作等業(yè)務(wù)。10年已為鹽池眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站制作公司優(yōu)惠進(jìn)行中。
用一個實例編寫一個 Python 程序,對列表進(jìn)行升序排序。
按升序排序列表的 Python 程序
這個 python 程序允許用戶輸入任何整數(shù)值,我們認(rèn)為它是一個列表的長度。接下來,我們使用 For 循環(huán)向 Python 列表添加數(shù)字。
Python 排序功能按照升序?qū)α斜眄椷M(jìn)行排序。
# Python Program to Sort List in Ascending Order
NumList = []
Number = int(input("Please enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
value = int(input("Please enter the Value of %d Element : " %i))
NumList.append(value)
NumList.sort()
print("Element After Sorting List in Ascending Order is : ", NumList)以升序輸出對 Python 列表進(jìn)行排序
Please enter the Total Number of List Elements: 4
Please enter the Value of 1 Element : 56
Please enter the Value of 2 Element : 76
Please enter the Value of 3 Element : 44
Please enter the Value of 4 Element : 2
Element After Sorting List in Ascending Order is : [2, 44, 56, 76]Python 程序以升序排序列表而不使用排序
在這個程序中,我們使用嵌套 For 循環(huán)來迭代列表中的每個數(shù)字,并按升序排序。
# Python Program to Sort List in Ascending Order
NumList = []
Number = int(input("Please enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
value = int(input("Please enter the Value of %d Element : " %i))
NumList.append(value)
for i in range (Number):
for j in range(i + 1, Number):
if(NumList[i] > NumList[j]):
temp = NumList[i]
NumList[i] = NumList[j]
NumList[j] = temp
print("Element After Sorting List in Ascending Order is : ", NumList)以升序輸出對 Python 列表進(jìn)行排序
Please enter the Total Number of List Elements: 4
Please enter the Value of 1 Element : 67
Please enter the Value of 2 Element : 86
Please enter the Value of 3 Element : 34
Please enter the Value of 4 Element : 55
Element After Sorting List in Ascending Order is : [34, 55, 67, 86]第一次 Python For Loop–第一次迭代:對于范圍(0,4) 中的 0,條件為真。因此,它進(jìn)入第二個 for 循環(huán)
嵌套循環(huán)–第一次迭代:對于范圍(0 + 1,4)中的 1, 條件為真。于是,進(jìn)入 If 語句
if(NumList[0]> NumList[1])= if(67 > 86)–表示條件為 False。因此,它從 If 塊退出,j 值增加 1。
嵌套循環(huán)–第二次迭代:對于范圍(1,4)中的 2–條件為真 如果(67>34)–條件為真 溫度= 67 數(shù)值列表[i] = 34 數(shù)值列表[j] = 67 現(xiàn)在列表= 34 86 67 55。接下來,j 遞增 1。
嵌套循環(huán)–第三次迭代:范圍(1,4)中的 3–條件為真
如果(34 > 55)–條件為假。因此,它從 If 塊退出,j 值為 4。
嵌套循環(huán)–第四次迭代:對于范圍(1,4)中的 4–條件為假 接下來,I 值增加 1。
第一次循環(huán)–第二次迭代:對于范圍(0,4) 中的 1,條件為真。因此,它進(jìn)入第二個循環(huán)
對剩余的 Python 迭代執(zhí)行相同的操作
使用 While 循環(huán)對列表進(jìn)行升序排序的 Python 程序
這個以升序排序列表項的 Python 程序與上面相同。然而,我們將 For Loop 替換為 While loop 。
# Python Program to Sort List in Ascending Order
NumList = []
Number = int(input("Please enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
value = int(input("Please enter the Value of %d Element : " %i))
NumList.append(value)
i = 0
while(i < Number):
j = i + 1
while(j < Number):
if(NumList[i] > NumList[j]):
temp = NumList[i]
NumList[i] = NumList[j]
NumList[j] = temp
j = j + 1
i = i + 1
print("Element After Sorting List in Ascending Order is : ", NumList) 本文名稱:Python程序:按升序排序列表
當(dāng)前地址:http://fisionsoft.com.cn/article/djecphd.html


咨詢
建站咨詢
