排序問題

60 views
Skip to first unread message

翁維陽

unread,
May 20, 2018, 10:13:04 AM5/20/18
to python.tw

各位專家

小弟剛步入程式圈想請教這一題怎麼答

我寫兩個func然後結合sorted但結果好像沒有排序,我不知道錯在哪

def num_total_sort(lst):

    n=0

    lst=str(lst)

    for i in range(len(lst)):    

        n+=int(lst[i:i+1])

    return n

def my_sort(s):

    sorted(s,key=num_total_sort)

    return s

s=[47,5556,63]

print(my_sort(s))




Buganini

unread,
May 20, 2018, 3:41:48 PM5/20/18
to pyth...@googlegroups.com
sorted()跟.sort()分清楚就ok了

--
這是 Google 網上論壇針對「python.tw」群組發送的訂閱通知郵件。
如要取消訂閱這個群組並停止接收來自這個群組的郵件,請傳送電子郵件到 pythontw+unsubscribe@googlegroups.com
如要在這個群組張貼留言,請傳送電子郵件到 pyth...@googlegroups.com
請前往以下網址造訪這個群組:https://groups.google.com/group/pythontw
如需更多選項,請前往:https://groups.google.com/d/optout

翁維陽

unread,
May 21, 2018, 1:23:52 AM5/21/18
to python.tw


拍謝 能在更詳細的解釋嗎
因為sort 和sorted 用法 大同小異
sorted 可以做的sort好像也可以

 

Peter. w

unread,
May 21, 2018, 2:19:45 AM5/21/18
to pyth...@googlegroups.com
def num_total_sort(lst):

    n=0

    lst=str(lst)

    for i in range(len(lst)):

        n+=int(lst[i:i+1])

    return n

def your_sort(s):

    sorted(s, key=num_total_sort)

    return sorted(s, key=num_total_sort)

def my_sort(s):

    s.sort(key=num_total_sort)


    return s

s=[47,5556,63]

print(my_sort(s))
print(your_sort(s))

--
Reply all
Reply to author
Forward
0 new messages