Python技巧:如何更改List和Dict的順序?

在特定條件下,我們希望能修改List和Dict資料型態的順序,具體上該怎麼做呢?

以下2則簡單的程式碼範例,介紹如何對List和Dict去更改順序:

範例1:假設name_list = [0, 1, 2, 3, 8],我們該如何將name_list改成[0, 1, 2, 3, 4]?

範例2:假設idx_to_class = {0: ‘good’, 2: ‘bad’},我們該如何將idx_to_class改成{0: ‘good’, 1: ‘bad’}?

--

--

Machine Learning | Deep Learning | https://linktr.ee/yanwei

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store