日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Python程序:按升序排序列表

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

創(chuàng)新互聯(lián)是一家專業(yè)提供鹽池企業(yè)網(wǎng)站建設,專注與網(wǎng)站建設、成都網(wǎng)站設計、成都h5網(wǎng)站建設、小程序制作等業(yè)務。10年已為鹽池眾多企業(yè)、政府機構(gòu)等服務。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站制作公司優(yōu)惠進行中。

用一個實例編寫一個 Python 程序,對列表進行升序排序。

按升序排序列表的 Python 程序

這個 python 程序允許用戶輸入任何整數(shù)值,我們認為它是一個列表的長度。接下來,我們使用 For 循環(huán)向 Python 列表添加數(shù)字。

Python 排序功能按照升序?qū)α斜眄椷M行排序。

# 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 列表進行排序

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 列表進行排序

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,條件為真。因此,它進入第二個 for 循環(huán)

嵌套循環(huán)–第一次迭代:對于范圍(0 + 1,4)中的 1, 條件為真。于是,進入 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,條件為真。因此,它進入第二個循環(huán)

對剩余的 Python 迭代執(zhí)行相同的操作

使用 While 循環(huá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)


網(wǎng)頁名稱:Python程序:按升序排序列表
當前路徑:http://www.5511xx.com/article/djecphd.html