Rumah >pembangunan bahagian belakang >Tutorial Python >Memahami susunan kamus Python
Bolehkah seseorang menerangkan cara kamus python (v3.5) diisih?
data = {"john": 23, "rick": 33, "mary": 63, "ron": 23, "joel": 51} for key in data: print("your name is " + key + ", and you are also " + str(data[key]) + " years old.")
Keluaran sebenar:
your name is rick, and you are also 33 years old. your name is ron, and you are also 23 years old. your name is mary, and you are also 63 years old. your name is john, and you are also 23 years old. your name is joel, and you are also 51 years old.
Output yang dijangkakan (urutan leksikografi):
Your name is John, and you are also 23 years old. Your name is Rick, and you are also 33 years old. Your name is Mary, and you are also 63 years old. Your name is Ron, and you are also 23 years old. Your name is Joel, and you are also 51 years old.
Ia bergantung pada versi Python yang anda gunakan.
Kamus menggunakan fungsi cincang asas untuk mengisih. Beberapa jenis mempunyai cincang masin, jadi ini bermakna anda mendapat pesanan yang berbeza pada setiap panggilan.
Kamus disusun mengikut susunan sisipan, iaitu kamus mengingati susunan item yang disisipkan. Daripada Dokumen:
Guido van Rossum mengumumkan dalam bahawa semua pelaksanaan Python mesti mengekalkan susunan sisipan bermula dengan Python 3.7.
Atas ialah kandungan terperinci Memahami susunan kamus Python. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!