python练习:字符串中的数字排序

#字符串中的字数排序
b = ‘1534788912‘
print ("将b排序显示,办法(列表):")
listb = list(b)                                             #[‘1‘, ‘5‘, ‘3‘, ‘4‘, ‘7‘, ‘8‘, ‘8‘, ‘9‘, ‘1‘, ‘2‘]
for i in  range(0, len(listb)-1):
    for j in range(0,len(listb)-1):
        if int(listb[j]) > int(listb[j+1]):
            listb[j], listb[j+1] = listb[j+1], listb[j]
    i+=1
print (listb)                                               #[‘1‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘7‘, ‘8‘, ‘8‘, ‘9‘]
newb = ‘‘
for item in  listb:
    newb = newb+item
print(newb)
#字符串中的字数排序
b = ‘1534788912‘
print ("将b排序显示,办法2(列表排序):")
listb = list(b)                                             #[‘1‘, ‘5‘, ‘3‘, ‘4‘, ‘7‘, ‘8‘, ‘8‘, ‘9‘, ‘1‘, ‘2‘]
listb.sort()
print (listb)                                               #[‘1‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘7‘, ‘8‘, ‘8‘, ‘9‘]
newb = ‘‘
for item in  listb:
    newb = newb+item
print(newb)                                                 #1123457889
#字符串中的字数排序b = ‘1534788912‘print ("将b排序显示,办法(列表):")listb = list(b)                                             #[‘1‘, ‘5‘, ‘3‘, ‘4‘, ‘7‘, ‘8‘, ‘8‘, ‘9‘, ‘1‘, ‘2‘]for i in  range(0, len(listb)-1):for j in range(0,len(listb)-1):if int(listb[j]) > int(listb[j+1]):listb[j], listb[j+1] = listb[j+1], listb[j]    i+=1print (listb)                                               #[‘1‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘7‘, ‘8‘, ‘8‘, ‘9‘]newb = ‘‘for item in  listb:newb = newb+itemprint(newb)

相关推荐