Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีสร้าง Ordered Dictionary ใน Python

OrderedDict เป็น subclass ของ dictionary ที่จดจำลำดับการเพิ่มคีย์-ค่า ถูกกำหนดในโมดูล collections ของไลบรารี Python OrderDict จดจำลำดับการเพิ่มคู่คีย์-ค่าใน dictionary

>>> from collections import OrderedDict
>>> od = OrderedDict(d.items())
>>> od
OrderedDict([('banana', 3), ('apple', 4), ('pear', 1), ('orange', 2)])
>>> t = od.popitem()
>>> t
('orange', 2)