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

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

新聞中心

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

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

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

用一個(gè)實(shí)例編寫(xiě)一個(gè) Python 程序,對(duì)列表進(jìn)行升序排序。

按升序排序列表的 Python 程序

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

Python 排序功能按照升序?qū)α斜眄?xiàng)進(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)

以升序輸出對(duì) 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 程序以升序排序列表而不使用排序

在這個(gè)程序中,我們使用嵌套 For 循環(huán)來(lái)迭代列表中的每個(gè)數(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)

以升序輸出對(duì) 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–第一次迭代:對(duì)于范圍(0,4) 中的 0,條件為真。因此,它進(jìn)入第二個(gè) for 循環(huán)

嵌套循環(huán)–第一次迭代:對(duì)于范圍(0 + 1,4)中的 1, 條件為真。于是,進(jìn)入 If 語(yǔ)句

if(NumList[0]> NumList[1])= if(67 > 86)–表示條件為 False。因此,它從 If 塊退出,j 值增加 1。

嵌套循環(huán)–第二次迭代:對(duì)于范圍(1,4)中的 2–條件為真 如果(67>34)–條件為真 溫度= 67 數(shù)值列表[i] = 34 數(shù)值列表[j] = 67 現(xiàn)在列表= 34 86 67 55。接下來(lái),j 遞增 1。

嵌套循環(huán)–第三次迭代:范圍(1,4)中的 3–條件為真

如果(34 > 55)–條件為假。因此,它從 If 塊退出,j 值為 4。

嵌套循環(huán)–第四次迭代:對(duì)于范圍(1,4)中的 4–條件為假 接下來(lái),I 值增加 1。

第一次循環(huán)–第二次迭代:對(duì)于范圍(0,4) 中的 1,條件為真。因此,它進(jìn)入第二個(gè)循環(huán)

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

使用 While 循環(huán)對(duì)列表進(jìn)行升序排序的 Python 程序

這個(gè)以升序排序列表項(xiàng)的 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)


當(dāng)前名稱:Python程序:按升序排序列表
文章路徑:http://m.5511xx.com/article/djecphd.html