新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Python 程序:按降序排序列表項(xiàng)目
創(chuàng)新互聯(lián)python教程:

在蘭考等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè) 網(wǎng)站設(shè)計(jì)制作定制制作,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),高端網(wǎng)站設(shè)計(jì),全網(wǎng)營銷推廣,外貿(mào)營銷網(wǎng)站建設(shè),蘭考網(wǎng)站建設(shè)費(fèi)用合理。
編寫一個(gè) Python 程序,按照降序?qū)α斜眄?xiàng)進(jìn)行排序。在這個(gè) Python 示例中,我們使用列表排序和反向方法對(duì)列表進(jìn)行降序排序。
intlist = []
intlistTot = int(input("Total Number of List Items to Sort = "))
for i in range(1, intlistTot + 1):
intlistvalue = int(input("Please enter the %d List Item = " %i))
intlist.append(intlistvalue)
intlist.sort()
intlist.reverse()
print('List Items After Sorting in Descending Order')
print(intlist)在這個(gè) Python 程序中,我們使用了嵌套 for 循環(huán)和 temp 變量對(duì)列表項(xiàng)進(jìn)行降序排序。
intlist = []
intlistTot = int(input("Total Number of List Items to Sort = "))
for i in range(1, intlistTot + 1):
intlistvalue = int(input("Please enter the %d List Item = " %i))
intlist.append(intlistvalue)
for i in range(len(intlist)):
for j in range(i + 1, len(intlist)):
if(intlist[i] < intlist[j]):
temp = intlist[i]
intlist[i] = intlist[j]
intlist[j] = temp
print('List Items After Sorting in Descending Order')
for i in range(len(intlist)):
print(intlist[i], end = ' ')Total Number of List Items to Sort = 8
Please enter the 1 List Item = 17
Please enter the 2 List Item = 47
Please enter the 3 List Item = 12
Please enter the 4 List Item = 99
Please enter the 5 List Item = 55
Please enter the 6 List Item = 4
Please enter the 7 List Item = 60
Please enter the 8 List Item = 75
List Items After Sorting in Descending Order
99 75 60 55 47 17 12 4Python 程序,使用 while 循環(huán)按降序?qū)α斜眄?xiàng)進(jìn)行排序。
intlist = []
intlistTot = int(input("Total Number of List Items to Sort = "))
i = 1
while(i <= intlistTot):
intlistvalue = int(input("Please enter the %d List Item = " %i))
intlist.append(intlistvalue)
i = i + 1
i = 0
while(i < len(intlist)):
j = i + 1
while j < len(intlist):
if(intlist[i] < intlist[j]):
temp = intlist[i]
intlist[i] = intlist[j]
intlist[j] = temp
j = j + 1
i = i + 1
print('List Items After Sorting in Descending Order')
i = 0
while(i < len(intlist)):
print(intlist[i], end = ' ')
i = i + 1Total Number of List Items to Sort = 7
Please enter the 1 List Item = 19
Please enter the 2 List Item = 55
Please enter the 3 List Item = 89
Please enter the 4 List Item = 32
Please enter the 5 List Item = 47
Please enter the 6 List Item = 15
Please enter the 7 List Item = 7
List Items After Sorting in Descending Order
89 55 47 32 19 15 7 網(wǎng)站欄目:Python 程序:按降序排序列表項(xiàng)目
轉(zhuǎn)載源于:http://fisionsoft.com.cn/article/dhopcph.html


咨詢
建站咨詢
