Pythonでリストを並べ替える方法

Sort()

リストは、sort().

並べ替え条件を選択する関数を実行する別のオプションを使用して自動的に昇順で並べ替えられます。

構文

list.sort(reverse=True|False、key = myFunc)< / span>< / div>

def myFunc(e):
return e['year']

cars = [
{'car': 'プジョー', '年': 2015},
{'car': '三菱', '年: 2010},
{'car': 'BMW', '年: 2022},
{'car': 'シトロエン', '年': 2012}
]

cars.sort(key=myFunc)

[{'car': '三菱', '年': 2010}, {'car': 'シトロエン', '年': 2012}, {'car': 'プジョー', '年': 2015}, {'car': 'BMW', '年': 2022}]

Commentaires (12)

Connectez-vous pour commenter

Rejoignez la discussion et partagez vos connaissances avec la communauté

JD
Jean Dupont Il y a 2 heures

Excellent tutoriel !

👍 12 Répondre Signaler
CodeurJava ✓ Auteur • Il y a 1 heure

N'hésitez pas si vous avez des questions.