我想知道如何對串列中的串列進行排序。但是,我不想按鍵對齊。我想根據以下方法更改它。
arr = [[2, 3], [5, 1], [4, 1], [5, 3], [4, 2]]
# solution...
I_want_arr = [[2, 3], [1, 5], [1, 4], [3, 5], [2, 4]]
我嘗試過這個
for i in arr:
i.sort()
但是,它沒有用
uj5u.com熱心網友回復:
使用串列理解:
arr = [[2, 3], [5, 1], [4, 1], [5, 3], [4, 2]]
sorted_output = [sorted(l) for l in arr]
使用map():
sorted_output = list(map(sorted, arr))
uj5u.com熱心網友回復:
@Gabip 的解決方案包括這個和一個更省時的解決方案,請先檢查一下!
怎么樣
arr = [[2, 3], [5, 1], [4, 1], [5, 3], [4, 2]]
I_want_arr = [sorted(x) for x in arr]
這輸出
[[2, 3], [1, 5], [1, 4], [3, 5], [2, 4]]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/314587.html
