مفاتيح القاموس (keys) والقيم (values)
انسخ الكود التالي إلى المحرر:
quantities = {'apple': 1, 'cat': 10}
print(quantities)
ثم غيّر print(quantities) إلى print(quantities.keys()) وشغّل البرنامج كاملًا.
الطريقة .keys() في القاموس تفعل تقريبًا ما تتوقعه: تعطيك مفاتيح القاموس. ويمكنك المرور على القيمة التي تعيدها بحلقة كما تمر على قائمة:
quantities = {'apple': 1, 'cat': 10}
for key in quantities.keys():
print(key)
في الحقيقة لا تحتاج حتى إلى .keys() في هذا السياق. المرور مباشرة على القاموس بحلقة يمر تلقائيًا على مفاتيحه.
أحيانًا تكون كتابة .keys() مفيدة لجعل المقصود أوضح للقارئ، لكنها ليست إلزامية. احذف .keys() من البرنامج وشغّله مرة أخرى.
يمكنك الآن استخدام هذه الفكرة لتعديل دالة المتجر من الجزء السابق والاستغناء عن معامل cart تمامًا:
def total_cost(quantities, prices):
result = 0
for item in ...:
price = prices[item]
quantity = quantities[item]
result += price * quantity
return result
check_result(
total_cost(
{'dog': 5000000, 'box': 2},
{'apple': 2, 'box': 5, 'cat': 100, 'dog': 100},
),
500000010,
)
في النسخة القديمة كان لدينا for item in cart، لكن cart لم تعد موجودة. قاموس quantities وحده يحدد المنتجات التي يشتريها العميل، لذلك مرّ على مفاتيحه.
تذكر أن we previously had
for item in cartin the function, butcartis no longer an argument.Now
quantitiesis the only argument that defines what the customer is buying.You need to iterate over the keys of
quantitiesinstead. تذكر أن 'iterate' here means 'loop over' with aforloop.You can use
.keys(), but you don't have to.
أصبح الحل أنظف، وانتهينا من حساب التكلفة الإجمالية.
لنرجع إلى مثال الترجمة. اكتب دالة تطبع كل كلمة إنجليزية في قاموس إنجليزي إلى فرنسي، ثم ترجمتها، مع تسمية كل لغة. هذا هو كود البداية:
def print_words(french):
...
print_words({'apple': 'pomme', 'box': 'boite'})
يجب أن يطبع الاستدعاء الأخير مثلًا:
English: apple French: pomme
English: box French: boite
مرّ على القاموس لتحصل على كل مفتاح، ثم استخدم المفتاح نفسه للحصول على القيمة المقابلة.
You will need to iterate (loop) over the dictionary.
You need to print both the key (English word) and the value (French word) of each dictionary entry.
You can get the value using the key in the same way as always.
رائع! لنضف قاموسًا ألمانيًا أيضًا:
def print_words(french, german):
...
print_words(
{'apple': 'pomme', 'box': 'boite'},
{'apple': 'apfel', 'box': 'kasten'},
)
يجب أن يطبع:
English: apple French: pomme German: apfel
English: box French: boite German: kasten
القاموسان لهما المفاتيح نفسها دائمًا، لكن القيم مختلفة. لذلك يكفي المرور على مجموعة مفاتيح واحدة، ثم استخدام المفتاح نفسه للحصول على الترجمتين.
This is still very similar to the previous exercise, nothing special yet.
You can reuse your previous solution, just add another argument and a tiny bit of code inside.
You now have to print one dictionary key and two dictionary values.
جميل! بدأ يظهر نمط واضح. بدل قاموسين منفصلين للغتين، يمكن دمجهما في قاموس متداخل كبير، بحيث تحتوي قيمة كل مفتاح على قاموس آخر للترجمات.
شغّل المثال التالي وتأمل بنية البيانات وطريقة الوصول إلى القيم الداخلية:
ممتاز 👌
بعد أن تعلمت قراءة القيم من القاموس، ستتعلم قريبًا كيفية تعيين قيم جديدة داخله.