قواميس Python

القاموس (Dictionary)

القاموس هو مجموعة غير مرتبة، قابلة للتغيير ولها ترتيب، مكتوبة باستخدام الدوائر، وتحتوي على مفاتيح وقيم. في بايثون، يتم كتابة القواميس باستخدام الدوائر، وتحتوي على مفاتيح وقيم.

مثال

إنشاء وتحديد القاموس:

thisdict =	{
  "brand": "Porsche",
  "model": "911",
  "year": 1963
}
print(thisdict)

Run Instance

الوصول إلى العنصر

يمكنك الوصول إلى العنصر في القاموس باستخدام اسم المفتاح المحدد في الدوائر:

مثال

الحصول على قيمة المفتاح 'model':

x = thisdict["model"]

Run Instance

هناك أيضًا get() الطريقة ستعطيك نفس النتيجة:

مثال

الحصول على قيمة المفتاح 'model':

x = thisdict.get("model")

Run Instance

تغيير القيمة

يمكنك تغيير قيمة العنصر المحدد باستخدام اسم المفتاح المحدد:

مثال

تغيير 'year' إلى 2019:

thisdict =	{
  "brand": "Porsche",
  "model": "911",
  "year": 1963
}
thisdict["year"] = 2019

Run Instance

استكشاف القاموس

يمكنك استخدام for تمرير الدائرة حول القاموس.

عند تمرير الدائرة حول القاموس، تعود القيمة هي المفتاح، ولكن هناك أيضًا طرق لاسترداد القيمة:

مثال

طباعة الأسماء المفتاحية بشكل فردي:

for x in thisdict:
  print(x)

Run Instance

مثال

طباعة القيم الموجودة في القاموس بشكل فردي:

for x in thisdict:
  print(thisdict[x])

Run Instance

مثال

يمكنك أيضًا استخدام values() تعود دالة values() القيم من القاموس:

for x in thisdict.values():
  print(x)

Run Instance

مثال

استخدام دالة items() لاستكشاف المفاتيح والقيم:

for x, y in thisdict.items():
  print(x, y)

Run Instance

تحقق من وجود المفتاح

لتحديد ما إذا كان المفتاح المحدد موجودًا في القاموس، استخدم: في الكلمة المفتاحية:

مثال

تحقق من وجود 'model' في القاموس:

thisdict =	{
  "brand": "Porsche",
  "model": "911",
  "year": 1963
}
if "model" in thisdict:
  print("نعم، 'model' هو أحد المفاتيح في قاموس thisdict")

Run Instance

طول القاموس

لتحديد عدد العناصر في القاموس (الملفات المفتاح-القيمة)، استخدم: len() الطريقة.

مثال

طباعة عدد العناصر في القاموس:

print(len(thisdict))

Run Instance

إضافة العنصر

يمكن إضافة العنصر إلى القاموس باستخدام مفتاح جديد وأسند له قيمة:

مثال

thisdict =	{
  "brand": "Porsche",
  "model": "911",
  "year": 1963
}
thisdict["color"] = "red"
print(thisdict)

Run Instance

إزالة العنصر

هناك عدة طرق لإزالة العناصر من القاموس:

مثال

يستخدم دالة pop() إزالة العنصر الذي يحتوي على الاسم المحدد للملف:

thisdict =	{
  "brand": "Porsche",
  "model": "911",
  "year": 1963
}
thisdict.pop("model")
print(thisdict)

Run Instance

مثال

popitem() إزالة العنصر الأخير المضمن (في إصدارات قبل 3.7، إزالة العنصر العشوائي):

thisdict =	{
  "brand": "Porsche",
  "model": "911",
  "year": 1963
}
thisdict.popitem()
print(thisdict)

Run Instance

مثال

del thisdict["model"] thisdict.popitem()}

thisdict =	{
  "brand": "Porsche",
  "model": "911",
  "year": 1963
}
الكلمة المفتاحية حذف العنصر الذي يحتوي على اسم المفتاح المحدد:
print(thisdict)

Run Instance

مثال

del thisdict["model"] del

thisdict =	{
  "brand": "Porsche",
  "model": "911",
  "year": 1963
}
الكلمة المفتاحية أيضًا يمكن أن تزيل الدictionary بالكامل:
del thisdict

Run Instance

مثال

clear() print(thisdict) #this 会导致错误،因为 "thisdict" 不再存在。

thisdict =	{
  "brand": "Porsche",
  "model": "911",
  "year": 1963
}
الكلمة المفتاحية تنظيف الدictionary:
print(thisdict)

Run Instance

thisdict.clear()

نسخ الدictionary لا يمكنك نسخ الدictionary من خلال dict2 = dict1التعديلات التي تتم عليها ستتأثر أيضًا في للنسخ الدictionary، لأن: المرجع، بينما فقط نسخ المرجع، بينما dict1 التعديلات التي تتم عليها ستتأثر أيضًا في dict2

中进行 copy().

مثال

استخدم copy() يوجد بعض الطرق للنسخ، وهي استخدام طريقة الدictionary المدمجة

thisdict =	{
  "brand": "Porsche",
  "model": "911",
  "year": 1963
}
mydict = thisdict.copy()
print(mydict)

Run Instance

يمكن أيضًا استخدام طريقة أخرى للنسخ باستخدام طريقة مدمجة dict().

مثال

استخدم dict() يخلق الطريقة دictionary نسخة من الدictionary:

thisdict =	{
  "brand": "Porsche",
  "model": "911",
  "year": 1963
}
mydict = dict(thisdict)
print(mydict)

Run Instance

دictionary مضمن

يمكن أن يحتوي الدictionary أيضًا على العديد من الدictionaries، ويُطلق عليها دictionary مضمن.

مثال

أنشئ دictionary يحتوي على ثلاثة دictionaries:

myfamily = {
  "child1" : {
    "name" : "Phoebe Adele",
    "year" : 2002
  },
  "child2" : {
    "name" : "Jennifer Katharine",
    "year" : 1996
  },
  "child3" : {
    "name" : "Rory John",
    "year" : 1999
  }
}

Run Instance

أو، إذا كنت ترغب في دمج ثلاثة دictionaries موجودة مسبقًا كدictionaries:

مثال

أنشئ ثلاثة دictionaries، ثم أنشئ دictionary يحتوي على الثلاثة دictionaries الأخرى:

child1 = {
  "name" : "Phoebe Adele",
  "year" : 2002
}
child2 = {
  "name" : "Jennifer Katharine",
  "year" : 1996
}
child3 = {
  "name" : "Rory John",
  "year" : 1999
}
myfamily = {
  "child1" : child1,
  "child2" : child2,
  "child3" : child3
}

Run Instance

بناء الدالة dict()

يمكن أيضًا استخدام dict() يخلق بناء الدالة دictionary جديدًا:

مثال

thisdict = dict(brand="Porsche", model="911", year=1963)
# من فضلك، الكلمات المفتاحية ليست نصية
# من فضلك، استخدمت المساواة بدلاً من العلامة المزدوجة للتعريف
print(thisdict)

Run Instance

Dictionary Methods

Python provides a set of built-in methods that can be used on dictionaries.

Method Description
clear() Remove all elements from the dictionary
copy() Return a copy of the dictionary
fromkeys() Return a dictionary with the specified key and value
get() Return the value for the specified key
items() Return a list of tuples containing each key-value pair
keys() Return a list containing all the keys of the dictionary
pop() Remove the element with the specified key
popitem() Remove the last inserted key-value pair
setdefault() Return the value for the specified key. If the key does not exist, insert a key with the specified value.
update() Update the dictionary with the specified key-value pairs
values() Return a list of all values in the dictionary