是否可以对代表菲律宾数字的字符串列表进行排序

2024-06-09 12:25:23 发布

您现在位置:Python中文网/ 问答频道 /正文

list=[dalawa,tatlo,apat,siyam,isa],是否有方法将其排序为list=[isa,dalawa,tatlo,apat,siyam]。我对python有一个新的了解,所以对此我没有任何想法


Tags: 方法排序listisasiyamtatloapatdalawa
1条回答
网友
1楼 · 发布于 2024-06-09 12:25:23

python sort()方法将按字母顺序对列表进行排序。 您可以将每个菲律宾数字的值指定为字典,然后根据值对其进行排序

应该这样做:(我在编数值)

list = {"dalawa":2, "tatlo":3, "apat":4, "siyam":5, "isa":1}

# look up lambda functions in order to better understand the below functionality. 
# In short what this does is, return to the sorted function the values of keys in the above dictionary and telling it to sort by them and not by the actual via resetting the key parameter to the lambda function.

result = sorted(list, key=lambda x:list[x[0]])

相关问题 更多 >