python列表内数字排序_如何在Python中手动排序数字列表?
規格:Ubuntu 13.04,Python 3.3.1
背景:Python的初學者,遇到了這個“手動排序”的問題。
我被要求做的是:“讓用戶輸入3個數值并將它們存儲在3個不同的變量中。不使用列表或排序算法,手動將這3個數字從最小值排序到最大值。”
我能想到的是:number = input("Please enter 3 numbers: ")
number = list(number)
a = int(number[0])
b = int(number[1])
c = int(number[2])
new_l = []
if a > b and a > c:
new_l.append(a)
if b > c:
new_l.append(b)
new_l.append(c)
else:
new_l.append(c)
new_l.append(b)
print(new_l)
if b > a and b > c:
new_l.append(b)
if a > c:
new_l.append(a)
new_l.append(c)
else:
new_l.append(c)
new_l.append(a)
print(new_l)
if c > a and c > b:
new_l.append(c)
if a > b:
new_l.append(a)
else:
new_l.append(b)
new_l.append(a)
print(new_l)
所以我的問題是:
我意識到我的解決方案非常有限。首先,它只能處理3個單個數字,因為一旦輸入字符串被轉換成一個列表,就無法將所有數字正確地分解成用戶想要的單個數字。第二,通過使用這個解決方案,編碼器被迫枚舉3個數字相互比較的所有可能場景,這可能非常不靈活,如果說,腳本將被更改為接受用戶輸入的100多個數字。
如果你能分享一些關于上述問題的指導,或者關于如何以不同的方式解決這個問題,我會非常高興!謝謝您。
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的python列表内数字排序_如何在Python中手动排序数字列表?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: sql取字段前4位_4千左右预算,202
- 下一篇: jdbc preparestatemen