آشنایی با دیکشنری ها در پایتون
در این قسمت به نوع چهارم مجموعه ها، یعنی دیکشنری ها در پایتون رسیده ایم. dictionary ها مجموعه هایی نامنظم، قابل تغییر و index شده هستند که با curly braces و مقادیر key/value (شبیه به آرایه های متناظر در زبان های دیگر) نوشته می شوند. به مثال زیر توجه کنید:
thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } print(thisdict)
خروجی:
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}
دسترسی به اعضا و تغییر مقدار آن ها
احتمال دارد با خودتان بگویید از آنجایی که dictionary ها نامنظم هستند بنابراین index ای ندارند که از طریقش به اعضای آن ها دسترسی داشته باشیم. اگر شما هم اینطور فکر می کنید باید بگویم چیز مهمی را فراموش کرده اید؛ مقادیر dictionary ها به صورت key/value هستند بنابراین key نقش همان index را دارد:
thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } x = thisdict["model"] print(x)
خروجی کلمه ی Mustang است.
متدی به نام ()get
نیز وجود دارد که به شما همین نتیجه را می دهد:
thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } x = thisdict.get("model") print(x)
خروجی باز هم کلمه ی Mustang است.
با این حساب تغییردادن اعضای set کار راحتی می شود؛ کافیست عضو مورد نظر را هدف گرفته و مقدار جدید را برایش قرار دهیم:
thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } thisdict["year"] = 2018 print(thisdict)
خروجی:
{'brand': 'Ford', 'model': 'Mustang', 'year': 2018}
گردش درون dictionary با حلقه ی for
شما می توانید با استفاده از یک حلقه ی for در dictionary ها گردش کنید اما باید به یاد داشته باشید که مقادیر برگشت داده شده در این حالت key ها خواهند بود! البته روش هایی برای برگرداندن value ها نیز وجود دارد.
مثال زیر به جای نمایش value ها تک تک key ها را نمایش می دهد:
thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } for x in thisdict: print(x)
خروجی:
brand
model
year
برای نمایش value ها باید کدمان را اینطور اصلاح کنیم:
thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } for x in thisdict: print(thisdict[x])
خروجی:
Ford
Mustang
1964
البته راه دیگری نیز وجود دارد: شما می توانید از تابع ()values
برای دریافت مقادیر dictionary ها استفاده کنید. به مثال زیر توجه کنید:
thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } for x in thisdict.values(): print(x)
خروجی:
Ford
Mustang
1964
اگر می خواهید هم key ها و هم value ها را داشته باشید از تابع ()items
استفاده کنید:
thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } for x, y in thisdict.items(): print(x, y)
خروجی:
brand Ford
model Mustang
year 1964
بررسی وجود key و تعداد اعضای dictionary
برخی اوقات ممکن است بخواهیم به دنبال key خاصی بگردیم. برای این کار از کلیدواژه ی in
استفاده می کنیم:
thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } if "model" in thisdict: print("Yes, 'model' is one of the keys in the thisdict dictionary")
خروجی:
Yes, 'model' is one of the keys in the thisdict dictionary
برای محاسبه ی تعداد اعضای dictionary ها مثل همیشه از تابع ()len
استفاده می کنیم:
thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } print(len(thisdict))
طبیعتا خروجی عدد 3 است.
اضافه کردن و حذف اعضای dictionary ها
برای اضافه کردن یک عضو جدید به dictionary ها باید یک key جدید را انتخاب کرده و مقداری را به آن انتساب دهیم:
thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } thisdict["color"] = "red" print(thisdict)
خروجی:
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964, 'color': 'red'}
برای حذف کردن اعضا نیز روش های مختلفی وجود دارد. به طور مثال ()pop مقدار key را گرفته و آن عضو را حذف می کند:
thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } thisdict.pop("model") print(thisdict)
خروجی:
{'brand': 'Ford', 'year': 1964}
همچنین ()popitem
آخرین عضو اضافه شده را حذف می کند:
thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } thisdict.popitem() print(thisdict)
خروجی:
{'brand': 'Ford', 'model': 'Mustang'}
همچنین کلیدواژه ی del
نیز یک key گرفته و عضو متناظرش را حذف می کند:
thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } del thisdict["model"] print(thisdict)
خروجی:
{'brand': 'Ford', 'year': 1964}
مثال:
thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } del thisdict print(thisdict)
خروجی خطای زیر خواهد بود:
NameError: name 'thisdict' is not defined
در آخر هم ()clear
وظیفه ی خالی کردن تمامی اعضای dictionary را بر عهده دارد:
thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } thisdict.clear() print(thisdict)
خروجی:
{}
کپی کردن dictionary
همانطور که برای لیست ها گفته بودیم list1 = list2 کار نمی کند باید برای dictionary ها هم بگوییم که dict2 = dict1 باعث کپی شدن dictionary نخواهد شد. به حای آن باید از توابع ()copy و ()dict استفاده کنیم. نمونه ی استفاده از ()copy:
thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } mydict = thisdict.copy() print(mydict)
خروجی:
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}
نمونه ی استفاده از تابع ()dict:
thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } mydict = dict(thisdict) print(mydict)
خروجی:
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}
dictionary های تو در تو (nest شده)
ما می توانیم dictionary ها را به صورت تو در تو نیز طراحی کنیم:
myfamily = { "child1" : { "name" : "Emil", "year" : 2004 }, "child2" : { "name" : "Tobias", "year" : 2007 }, "child3" : { "name" : "Linus", "year" : 2011 } } print(myfamily)
خروجی:
{'child1': {'name': 'Emil', 'year': 2004}, 'child2': {'name': 'Tobias', 'year': 2007}, 'child3': {'name': 'Linus', 'year': 2011}}
و اگر موارد دیگر در حال حاضر dictionary هستند می توان گفت:
child1 = { "name" : "Emil", "year" : 2004 } child2 = { "name" : "Tobias", "year" : 2007 } child3 = { "name" : "Linus", "year" : 2011 } myfamily = { "child1" : child1, "child2" : child2, "child3" : child3 } print(myfamily)
خروجی:
{'child1': {'name': 'Emil', 'year': 2004}, 'child2': {'name': 'Tobias', 'year': 2007}, 'child3': {'name': 'Linus', 'year': 2011}}
در آخر باید بگویم که dictionary ها نیز دارای constructor خاص خود هستند:
thisdict = dict(brand="Ford", model="Mustang", year=1964) # note that keywords are not string literals # note the use of equals rather than colon for the assignment print(thisdict)
خروجی:
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}
توجه داشته باشید که در این حالت key ها را درون quote (علامت ""
) نمیگذاریم. امیدوارم از این قسمت لذت برده باشید.